How to Build an AI agent
Build custom AI agents with persistent instructions, knowledge bases, connected tools, and multi-agent workflows using Squid's client SDK.
Why Build an AI Agent with Squid
Adding AI capabilities to your app typically means stitching together an LLM API, a vector database for context retrieval, tool-calling logic, conversation memory, and security rules. Each piece requires its own integration work.
Squid handles all of this in a unified platform. You define an agent with instructions and abilities, connect it to your data sources and tools, and interact with it through a single SDK. Squid manages prompt construction, context retrieval, memory, and orchestration so you can focus on the experience you want to create.
How it Works
Under the hood, agents use a Large Language Model (LLM) to generate answers to users' questions. When a user asks a question, the persistent instructions and most relevant context are passed to the LLM as part of the prompt, providing the user with a contextualized answer.
Squid lets you choose the LLM for your AI agent, allowing you to find the best fit for your use case. The following LLM providers are available out of the box:
You can also connect to additional providers by adding an AI connector. This allows you to use self-hosted models (e.g., Ollama, vLLM), AWS Bedrock models, or any other OpenAI-compatible endpoint.
Building an Agent
An agent represents a distinct personality or setup for your AI workflow. Each agent is like a different persona or use case, distinguished by its own set of instructions and abilities. This design enables customized responses from the AI according to the specific agent.
The following examples show how to create an agent using Squid's SDKs. If you are unfamiliar with developing using the Squid platform and SDKs, please read our documentation on fullstack development.
Upserting an Agent
To create or update an AI agent programmatically, use the upsert() method, specifying which agent ID should be created or updated:
await squid
.ai()
.agent('banking-copilot')
.upsert({
options: {
model: 'gpt-5.5',
},
isPublic: true,
});
When inserting an agent, pass an options object with a model field indicating the model the agent will use.
The isPublic parameter determines whether the chat functionality of the given agent can be accessed without setting up security rules.
Deleting an Agent
To delete an existing agent, use the delete() method:
await squid.ai().agent('banking-copilot').delete();
This function results in an error if no agent exists for the provided agent ID.
Updating the Model
To change the LLM model an agent uses, call updateModel():
await squid.ai().agent('banking-copilot').updateModel('claude-sonnet-4-6');
The model can be a vendor model name (e.g. 'gpt-5.5', 'claude-sonnet-4-6', 'gemini-3.5-flash') or an integration-based model for additional providers like Ollama, AWS Bedrock, or any OpenAI-compatible endpoint:
await squid.ai().agent('banking-copilot').updateModel({
integrationId: 'my-ollama',
model: 'llama3',
});
You can also select a CLI agent model such as 'claude-code' or 'codex' to run long-running, tool-driven agentic asks.
You can also override the model per-request when calling ask() or chat() using the model option. See Ask Options for details.
Listing Agents and Resources
The squid.ai() client exposes discovery methods for everything defined in your app:
| TypeScript | Python | Returns |
|---|---|---|
listAgents() | list_agents() | All agents with their configuration |
listKnowledgeBases() | list_knowledge_bases() | All knowledge bases |
listChatModels() | list_chat_models() | Available chat models. Pass includeDeprecated: true to include deprecated vendor models. |
listFunctions() | list_functions() | Registered AI functions |
- TypeScript
- Python
const agents = await squid.ai().listAgents();
const models = await squid.ai().listChatModels({ includeDeprecated: false });
agents = await squid.ai().list_agents()
models = await squid.ai().list_chat_models(include_deprecated=False)
Each method returns an array of records with the following contents:
- Agents (
AiAgent):id,createdAt,updatedAt,description,isPublic,auditLog, andoptions(the agent's full configuration, including its model and connected knowledge bases, integrations, and functions). - Knowledge bases (
AiKnowledgeBase):id,name,description,embeddingModel,chatModel,metadataFields,createdAt, andupdatedAt. Knowledge bases created by connector syncs may carry additional sync bookkeeping fields. - Chat models (
ModelIdSpec):modelId(the string to pass as a model selection),displayName,description, andsource('vendor','connector', or'custom'). Integration-based models includeintegrationId, and deprecated models includereplacedBynaming the active model that calls are routed to. - AI functions (
AiFunctionMetadata):serviceFunction(ServiceName:functionName),description, andparams(each withname,type,description, andrequired). Connector-provided functions carryattributes.integrationType, and entries flaggedinternalare not meant for direct use.
Setting the Agent Description
To set or update a human-readable description for the agent, use the setAgentDescription() method. This updates only the description without affecting other agent configuration:
await squid
.ai()
.agent('banking-copilot')
.setAgentDescription('Assists customer support staff with banking and finance questions');
Alternatively, you can use upsert() to set the description along with all other agent values in a single call. Note that upsert() replaces the entire agent configuration, so any fields not included will be cleared.
Instructions
Instructions set the rules for how the agent responds to prompts and answers questions. They should be direct and simple, and explain the purpose of the agent. Instructions are supplied as a block of text.
Adding Instructions
To add or edit instructions for an AI agent, use the updateInstructions() method, passing the instruction data as a string.
const instruction = 'You are a helpful copilot that assists customer support staff by providing answers to their questions about banking and finance products.';
await squid.ai().agent('banking-copilot').updateInstructions(instruction);
Connected Knowledge Bases
Knowledge bases store the searchable context an agent draws on when answering questions. To create a knowledge base and manage its context and metadata, see the Knowledge Bases documentation.
Connect a Knowledge Base to an Agent
Use setAgentOptionInPath() to give an agent access to one or more knowledge bases without affecting other agent configuration. The description tells the agent when to consult each knowledge base:
await squid
.ai()
.agent('banking-copilot')
.setAgentOptionInPath('connectedKnowledgeBases', [
{
knowledgeBaseId: 'banking-knowledgebase',
description: 'Use for information on credit cards',
},
]);
To disconnect all knowledge bases, pass an empty array:
await squid
.ai()
.agent('banking-copilot')
.setAgentOptionInPath('connectedKnowledgeBases', []);
You can also use upsert() to set connected knowledge bases along with all other agent values in a single call. Note that upsert() replaces the entire agent configuration, so any fields not included will be cleared.
Each connected knowledge base entry supports the following fields:
| Field | Type | Required | Description |
|---|---|---|---|
knowledgeBaseId | string | Yes | The knowledge base to connect |
description | string | Yes | Tells the agent when to consult this knowledge base |
includeMetadata | boolean | No | Include document metadata with search results provided to the agent. When omitted, falls back to the deprecated ask-level includeMetadata option, which defaults to true; set to false to exclude metadata. |
enableMetadataInspection | boolean | No | Give the agent a tool to enumerate and search this knowledge base's metadata field values. Defaults to false. See agent-driven metadata filtering. |
await squid
.ai()
.agent('banking-copilot')
.setAgentOptionInPath('connectedKnowledgeBases', [
{
knowledgeBaseId: 'banking-knowledgebase',
description: 'Use for information on credit cards',
includeMetadata: true,
enableMetadataInspection: true,
},
]);
The ask-level includeMetadata option still works but is deprecated in favor of the per-knowledge-base flag.
Interacting with your Agent
Once an agent has been created, you're ready to start asking questions or giving prompts.
Getting a Full Response with ask()
Use the ask() method to send a prompt and receive the complete response as a string:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('Which credit card is best for students?');
Getting Responses with Annotations
Use askWithAnnotations() to receive the response along with any file annotations (e.g., generated images or documents):
const { responseString, annotations } = await squid
.ai()
.agent('banking-copilot')
.askWithAnnotations('Generate a comparison chart of our credit cards');
Streaming Responses with chat()
Use the chat() method to stream responses token by token. This returns an RxJS Observable<string> that emits the accumulated response as each token arrives, which is ideal for displaying real-time responses in a UI:
import { Subscription } from 'rxjs';
const stream = squid
.ai()
.agent('banking-copilot')
.chat('Which credit card is best for students?');
const subscription: Subscription = stream.subscribe({
next: (accumulatedResponse) => {
// Each emission contains the full response so far
console.log(accumulatedResponse);
},
complete: () => {
console.log('Response complete');
},
error: (err) => {
console.error('Error:', err);
},
});
The chat() method accepts the same options as ask() (except voiceOptions), plus an additional smoothTyping option (defaults to true) that adds a slight delay between tokens for a natural typing effect.
Ask Options
Both ask() and chat() accept an optional options parameter to configure the request. To view a full list of available options and their default values, refer to the API reference documentation.
await squid.ai().agent('banking-copilot').ask('Which credit card is best for students?', {
maxOutputTokens: 4096,
temperature: 0.7,
model: 'claude-sonnet-4-6',
});
Memory and Chat History
By default, agents remember previous messages within a session. Use memoryOptions to control this behavior:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('What did I ask earlier?', {
memoryOptions: {
memoryMode: 'read-write', // 'none' | 'read-only' | 'read-write'
memoryId: 'user-123-session', // Unique ID for this conversation
expirationMinutes: 60, // How long to keep the history
},
});
'none': No history is used. Each prompt is answered independently.'read-only': The agent can reference past messages but will not save new ones.'read-write': The agent reads and writes to history (default behavior).
The memoryId identifies a conversation. Using the same memoryId across requests continues the same conversation. Treat memory IDs with the same security as access tokens since they grant access to the chat history.
To retrieve past messages for a given conversation, use getChatHistory():
const messages = await squid
.ai()
.agent('banking-copilot')
.getChatHistory('user-123-session');
Response Format
Control the format of the agent's response using responseFormat:
// Get a JSON response
const json = await squid
.ai()
.agent('banking-copilot')
.ask('List our credit cards with their fees', {
responseFormat: 'json_object',
});
// Get a response that strictly conforms to a JSON schema (Anthropic models)
const structured = await squid
.ai()
.agent('banking-copilot')
.ask('Analyze the sentiment of this review', {
model: 'claude-sonnet-4-6',
responseFormat: {
type: 'json_schema',
schema: {
type: 'object',
properties: {
sentiment: { type: 'string', enum: ['positive', 'negative', 'neutral'] },
confidence: { type: 'number' },
},
required: ['sentiment', 'confidence'],
},
},
});
Available formats:
'text'(default): Plain text response.'json_object': The model attempts to return valid JSON.{ type: 'json_schema', schema: ... }: Structured output that guarantees the response conforms to the provided JSON schema. Currently supported by Anthropic models.
Including Files in the Prompt
Pass images or documents as part of the prompt using fileUrls:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('What does this document say?', {
fileUrls: [
{
id: 'doc-1',
type: 'document',
purpose: 'context',
url: 'https://example.com/statement.pdf',
description: 'Customer bank statement',
},
],
});
Each file URL requires an id (unique per request), a type ('image' or 'document'), and a purpose:
'context': The file is included directly in the prompt for the AI to reference.'tools': The file is returned as part of a tool/function call result.
Overriding the Model Per-Request
Override the agent's default model for a single request:
const response = await squid.ai().agent('banking-copilot').ask('Summarize this data', {
model: 'gpt-5.5',
});
Additional Options
| Option | Type | Default | Description |
|---|---|---|---|
maxTokens | number | Model max | Maximum input tokens Squid can send to the model |
maxOutputTokens | number | - | Maximum tokens the model should generate |
temperature | number | 0.5 | Sampling temperature (0-1) |
timeoutMs | number | 240000 | Request timeout in milliseconds |
instructions | string | - | Additional instructions appended to the agent's default instructions |
guardrails | object | - | Override guardrail settings per-request |
disableContext | boolean | false | Skip knowledge base context for this request |
includeReference | boolean | false | Include source references in the response |
reasoningEffort | string | - | 'minimal', 'low', 'medium', or 'high' (for reasoning models) |
useCodeInterpreter | string | 'none' | 'llm' to enable Python code execution (OpenAI and Gemini only) |
executionPlanOptions | object | - | Enable the agent to plan before acting |
Filtering Context with Metadata
When you have added metadata to your context, you can use the contextMetadataFilterForKnowledgeBase chat option to instruct the AI agent to only consult specific contexts. Only contexts that meet the filter requirement will be used to respond to the client prompt.
The following example filters contexts to only include those with a metadata value of "company" that is equal to "Bank of America":
await squid
.ai()
.agent('banking-copilot')
.ask('Which Bank of America credit card is best for students?', {
contextMetadataFilterForKnowledgeBase: {
['banking-knowledgebase']: { company: { $eq: 'Bank of America' } },
},
});
For the full list of supported filter operators, combining filters with $and and $or, and how agents construct filters on their own, see Filtering Knowledge Base Context with Metadata.
AI Functions
Squid AI Agents can handle specific use cases and create more consistent responses using AI functions.
Adding Functions to an Agent
You can attach AI functions to an agent using setAgentOptionInPath(), which updates only the function list without affecting other agent configuration:
await squid
.ai()
.agent('banking-copilot')
.setAgentOptionInPath('functions', ['getCreditLimit']);
To update the function list, call setAgentOptionInPath() again with the new set of functions. Passing an empty array removes all functions. You can also use upsert() to set functions along with all other agent values in a single call, but note that upsert() replaces the entire agent configuration.
Passing Functions at Ask Time
Alternatively, pass AI function names per-request using the functions option. This overrides the agent's stored function list for that request:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('What is my current credit limit?', {
functions: ['getCreditLimit', 'getAccountBalance'],
});
To learn more about AI functions, view the documentation. To see an example application that uses AI functions, check out this AI agent tutorial.
Connected Agents
Agents can delegate tasks to other agents, enabling multi-agent workflows. A connected agent appears as a callable tool that the parent agent can invoke when it determines the sub-agent is best suited to handle a specific part of the user's request.
Configuring Connected Agents
Use updateConnectedAgents() to set the list of agents connected to this agent. The description tells the parent agent when to delegate to each connected agent:
await squid
.ai()
.agent('banking-copilot')
.updateConnectedAgents([
{
agentId: 'fraud-detection-agent',
description: 'Call this agent when the user asks about suspicious transactions or potential fraud',
},
]);
To disconnect all agents, pass an empty array:
await squid.ai().agent('banking-copilot').updateConnectedAgents([]);
Passing Connected Agents at Ask Time
You can also specify connected agents per-request, which overrides the stored configuration:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('Is this transaction suspicious?', {
connectedAgents: [
{
agentId: 'fraud-detection-agent',
description: 'Call this agent for fraud analysis',
},
{
agentId: 'compliance-agent',
description: 'Call this agent for regulatory compliance checks',
},
],
});
By default, nested agent calls can recurse up to 5 levels deep. You can adjust this using the quotas option:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('Analyze this portfolio', {
quotas: { maxAiCallStackSize: 3 },
});
Connected Integrations
Agents can connect to your data sources and external services, allowing them to query databases, call APIs, and interact with SaaS tools as part of answering a prompt.
Configuring Connected Integrations
Use setAgentOptionInPath() to give the agent access to connectors without affecting other agent configuration:
await squid
.ai()
.agent('banking-copilot')
.setAgentOptionInPath('connectedIntegrations', [
{
integrationId: 'my-postgres',
integrationType: 'postgres',
description: 'Use this database to look up customer account information',
},
]);
The description helps the agent understand when to use this integration. The integrationType must match the type of connector configured in the Squid Console.
To disconnect all integrations, call setAgentOptionInPath() with an empty array. You can also use upsert() to set connected integrations along with all other agent values in a single call, but note that upsert() replaces the entire agent configuration.
Passing Connected Integrations at Ask Time
Like connected agents, integrations can also be specified per-request:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('What are my recent transactions?', {
connectedIntegrations: [
{
integrationId: 'my-postgres',
integrationType: 'postgres',
description: 'Customer transaction database',
},
],
});
Execution Planning
For complex tasks involving multiple tools, connected agents, or integrations, you can enable execution planning. When enabled, the agent first creates a plan of what actions to take before executing them:
const response = await squid
.ai()
.agent('banking-copilot')
.ask('Compare our credit card offerings with competitor rates', {
executionPlanOptions: {
enabled: true,
reasoningEffort: 'high', // 'minimal' | 'low' | 'medium' | 'high'
allowClarificationQuestions: true, // Let the agent ask follow-up questions
},
});
You can optionally specify a different model for the planning step using the model field within executionPlanOptions.
Observing Status Updates
When an agent processes a complex request involving tool calls, connected agents, or integrations, you can observe real-time status updates via WebSocket:
const statusUpdates = squid.ai().agent('banking-copilot').observeStatusUpdates();
statusUpdates.subscribe({
next: (status) => {
console.log(`[${status.title}] ${status.body}`);
},
});
The returned Observable emits AiStatusMessage objects with title and body fields describing each step the agent takes.
observeStatusUpdates() also accepts an optional job ID: observeStatusUpdates(jobId) only emits updates for that specific request, which is useful when tracking a single long-running ask such as a CLI agent model run. Without an argument, it emits all status updates for the agent.
Agent Revisions
Every change to an agent's configuration automatically records a revision. Creating the agent records a created revision, each update (including upsert(), updateModel(), updateInstructions(), and setAgentOptionInPath()) records updated, and deleting the agent records a final deleted revision that preserves its last state. No setup is required.
Listing Revisions
Use listRevisions() to retrieve an agent's full history, sorted newest first:
- TypeScript
- Python
const { revisions } = await squid.ai().agent('banking-copilot').listRevisions();
console.log(revisions[0].revisionNumber, revisions[0].action);
revisions = await squid.ai().agent('banking-copilot').list_revisions()
print(revisions[0]['revisionNumber'], revisions[0]['action'])
Each revision contains:
| Field | Type | Description |
|---|---|---|
agentId | string | The agent this revision belongs to |
revisionNumber | number | Sequential number, incremented on each change |
action | 'created' | 'updated' | 'deleted' | The change that triggered the revision |
createdAt | Date | When the revision was recorded |
agentSnapshot | AiAgent | The full agent configuration at the time of the revision |
There is no separate change description; to see what changed, compare consecutive snapshots (the Squid Console does this for you).
Restoring and Deleting Revisions
Restore the agent to a previous revision with restoreRevision():
- TypeScript
- Python
await squid.ai().agent('banking-copilot').restoreRevision(3);
await squid.ai().agent('banking-copilot').restore_revision(3)
Restoring is non-destructive: the current state is first saved as a new revision, then the agent's configuration is replaced with the snapshot. History only grows, so a restore can itself be undone. A deleted revision cannot be restored.
To permanently remove a single revision, use deleteRevision():
- TypeScript
- Python
await squid.ai().agent('banking-copilot').deleteRevision(3);
await squid.ai().agent('banking-copilot').delete_revision(3)
Full method signatures are in the TypeScript and Python reference docs.
You can also browse revisions visually: the agent's page in the Squid Console has a Revisions tab showing what changed in each revision, with restore and delete actions.
Audit Log
Separate from configuration revisions, each agent keeps a runtime audit log of its invocations: the user prompt, tool and integration calls, knowledge base searches, the final response, and token usage.
Logging is controlled by the agent's auditLog field, which is enabled by default for new agents. Toggle it in the console or by including it in upsert():
- TypeScript
- Python
await squid
.ai()
.agent('banking-copilot')
.upsert({
options: { model: 'gpt-5.5' },
isPublic: true,
auditLog: false,
});
await squid.ai().agent('banking-copilot').upsert(
is_public=True,
audit_log=False,
options={'model': 'gpt-5.5'},
)
Reading the log is a console feature: open the Audit Log tab of the agent's page in the Squid Console. The log entries are not exposed through the SDK.
Error Handling
Common Errors
| Error | Cause | Solution |
|---|---|---|
| Agent not found | Calling delete(), get(), or other methods on a non-existent agent ID | Verify the agent ID exists by calling get() first, or use upsert() to ensure the agent is created |
| Context not found | Calling deleteContext() or getContext() with a context ID that does not exist | Use listContexts() to verify the context ID before deleting or retrieving |
| Request timeout | The agent takes longer than the configured timeoutMs (default: 4 minutes) | Increase timeoutMs in the options, simplify the prompt, or reduce the number of connected tools |
| Embedding model cannot be modified | Attempting to change the embeddingModel on an existing knowledge base | Create a new knowledge base with the desired embedding model instead |
Handling Errors in Streaming
When using chat(), errors are delivered through the Observable's error callback:
const stream = squid.ai().agent('banking-copilot').chat('Analyze this data');
stream.subscribe({
next: (response) => console.log(response),
error: (err) => {
console.error('Agent error:', err.message);
},
complete: () => console.log('Done'),
});
Best Practices
Instructions
- Keep instructions concise and direct. Describe the agent's role and what it should (and should not) do.
- Use instructions for behavioral rules (tone, scope, response style) rather than for factual content. Put factual content in knowledge bases instead.
- Test changes to instructions using the Test chat feature in the Agent Studio before deploying.
Knowledge Bases
- Use descriptive
descriptionvalues when connecting knowledge bases. The description is how the agent decides which knowledge base to consult, especially when multiple are connected.
For guidance on structuring knowledge base content and metadata, see the knowledge base best practices.
Multi-Agent Workflows
- Give each connected agent a clear, specific description. Vague descriptions lead to incorrect delegation.
- Set
quotas.maxAiCallStackSizeto a reasonable limit to avoid runaway recursive calls between agents. - Use
executionPlanOptionsfor complex multi-step tasks so the agent reasons about its approach before acting.
Performance
- Use
chat()for user-facing interactions where perceived speed matters. Streaming shows tokens as they arrive rather than waiting for the full response. - Set
disableContext: truewhen knowledge base context is not needed for a request to reduce latency. - Use
memoryOptions.memoryMode: 'none'for stateless, one-off requests that do not need conversation history.
Securing your Agent
Securing your data is vital when using the Squid Client SDK to create agents and enable chatting. The AI agent and the chats conducted with them may contain sensitive information, so it is crucial to restrict access and updates to prevent unauthorized usage or modification.
To learn about securing your AI agent, check out the Securing AI agents documentation.
Agent API Keys
Agent API Keys can provide a more granular level of security when calling Agent actions. View the Agent API Keys documentation for more details.