MATLAB® AI Agent SDK lets you build and run AI agents in MATLAB.
-
Create agents based on OpenAI®, Ollama™, or OpenAI-compatible APIs.
-
Integrate LLMs and agentic workflows into your workflows in a targeted manner, retaining deterministic workflows when those are more suitable.
-
Let your agent work on large amounts of data without needing to send the data to the LLM.
This SDK is a Research Preview under active development and APIs may change.
Please leave feedback, report bugs and feature requests via Issues. We review all contributions, but we do not merge external pull requests. See CONTRIBUTING.md for details.
You can use the add-on in MATLAB Online™ by clicking this link:
To use the add-on on an installed version of MATLAB, you can clone the GitHub repository. In the MATLAB Command Window, run this command:
>> !git clone https://github.com/matlab/matlab-ai-agent-sdk.git
To run code from the add-on outside of the installation directory, add the path to the installation directory.
>> addpath("path/to/matlab-ai-agent-sdk")
Using the OpenAI API requires an OpenAI API key. For information on how to obtain one, as well as pricing, terms and conditions of use, and available models, see the OpenAI documentation.
Set your key as an environment variable in a .env file:
OPENAI_API_KEY=<your key>
Then load it in MATLAB.
loadenv(".env")To connect to local or remote Ollama models, first install Ollama.
After you have installed Ollama, you can install models from the MATLAB Command Window:
!ollama pull <modelname>Create an LLM client by using the aisdk.LLMClient function and using the API and the model name as input arguments, for example:
clientOpenAI = aisdk.LLMClient("openai", "gpt-4.1-mini");
clientOllama = aisdk.LLMClient("ollama", "<model-name>");Then, generate text by using the generate function.
text = generate(client, "This is an example prompt.")text =
"This is an example reponse."
This example shows how to create a conversation with an LLM and automatically keep track of the message history.
Create the agent from an LLM client client by using the aisdk.AIAgent function. Provide a system prompt.
systemPrompt = "Reply as if you are writing telegrams.";
agent = aisdk.AIAgent(client,systemPrompt);Run the agent by using the run function. Provide a prompt.
prompt = "TOMATO FRUIT OR VEGETABLE STOP";
run(agent,prompt)ans =
"TOMATO TECHNICALLY A FRUIT STOP COMMONLY USED AS VEGETABLE IN CULINARY CONTEXT STOP END OF TRANSMISSION."
Ask a follow up question by using the run function.
run(agent,"HOW ABOUT AVOCADO STOP")ans =
"AVOCADO ALSO A FRUIT STOP KNOWN AS ALLIGATOR PEAR STOP HIGH IN HEALTHY FATS AND NUTRIENTS STOP END OF TRANSMISSION."
Inspect the chat history by using the Messages property of the agent.
agent.Messagesans =
1×4 LLMTextMessage array with messages:
1 User Text "TOMATO FRUIT OR VEGETABLE STOP"
2 Assistant Text "TOMATO TECHNICALLY A FRUIT STOP COMMONLY USED AS VEGETABLE I..."
3 User Text "HOW ABOUT AVOCADO STOP"
4 Assistant Text "AVOCADO ALSO A FRUIT STOP KNOWN AS ALLIGATOR PEAR STOP HIGH ..."
This example shows how to create an AI agent with a set of tools.
Create a function that counts the number of times a letter appears in a word.
function numLetter = countLetters(word,letter)
numLetter = count(word,letter);
endCreate a tool from the countLetters function by using the aisdk.LLMTool function. Add information about input and output arguments to the tool by using the aisdk.LLMToolArgument function.
tool = aisdk.LLMTool(@countLetters);
tool.InputArguments(1) = aisdk.LLMToolArgument("word",DataType="string");
tool.InputArguments(2) = aisdk.LLMToolArgument("letter",DataType="string");
tool.OutputArguments = aisdk.LLMToolArgument("numLetter",DataType="number");Create the agent from an LLM client client by using the aisdk.AIAgent function. Leave the system prompt empty.
systemPrompt = "";
agent = aisdk.AIAgent(client,systemPrompt,tool);Run the agent by using the run function.
run(agent,"How many times is the letter r in the word strawberry?")ans = "The letter "r" appears 3 times in the word "strawberry.""
This example shows how to configure an LLM tool to use data from the agent workspace as input or output data.
The eig function calculates the eigenvectors and eigenvalues of matrices. Vectors and matrices can contain a lot of numerical data. Instead of sending all this data to an LLM, which would cost tokens, keep the data in the agent workspace and configure your tools to work on that workspace.
Create a function called eigTool.
-
The first input argument of the function must be a structure array. Call the argument
ws. -
The last output argument of the function must be the same structure array.
To allow the agent to understand the outcome of the tool call, add another output argument, observation, that contains a natural language description of the outcome of the tool call. Describe the outcome using the observation output argument.
function [observation,ws] = eigTool(ws)
% Compute the eigenvalues of a matrix
ws.eigenvalues = eig(ws.matrix);
observation = "Eigenvalues added to the workspace as a variable called eigenvalues.";
endCreate an LLM tool from the eigTool function by using the aisdk.LLMTool function. Set the Workspace name-value argument to "agent".
tool = aisdk.LLMTool(@eigTool,Workspace="agent");You can now add the tool to an agent agentWithWorkspace. Add a matrix A to the agent workspace by setting the Workspace property. Call the field matrix to match the field name in the tool definition.
agentWithWorkspace.Tools = tool;
agentWithWorkspace.Workspace.matrix = A;| Function | Description |
|---|---|
| aisdk.AIAgent | Build AI agent |
| aisdk.LLMClient | Connect to third-party LLM API |
| aisdk.LLMTool | Tool for AI agent |
| aisdk.LLMToolArgument | Argument for LLM tool |
| aisdk.LLMMessage | Create LLM message |
| OpenAIClient | Client for OpenAI API |
| OllamaClient | Client for Ollama API |
| LocalLLMTool | Tool for AI agent from local function |
| MCPTool | Tool for AI agent from MCP server |
| LLMTextMessage | LLM message containing text |
| LLMImageMessage | LLM message containing image |
| LLMToolCallMessage | LLM message containing tool call |
| LLMToolResultMessage | LLM message containing tool result |
| Example | Description |
|---|---|
| CreateSimpleChatBotUsingAIAgent.m | Create interactive chatbot in Command Window |
| AnalyzeTextUsingParallelToolCalls.m | Extract structured data from text |
| FitPolynomialToDataUsingAIAgent.m | Build AI agent that fits polynomials to data (requires Curve Fitting Toolbox™) |
| NestedToolsAndSubagentsExample.m | Create tools that provide other tools |
| MCPClientAndAgentTools.m | Connect agent to Model Context Protocol (MCP) server |
The license is available in the LICENSE file in this GitHub repository.
To ask questions, report issues, or request technical support, open an Issue.
Copyright 2026 The MathWorks, Inc.