Building AI Agents · Part 4 of 7 · Context, Memory & Tooling · Oct 2026
Quick answer: Context is everything an agent knows when it makes a decision: its instructions, the conversation, files, memory and tool results. Memory lets it carry information across conversations, and tools let it act beyond text, for example by searching, querying a database or sending a message. Tools are what make an agent autonomous.
Agent Context
Agent context is the information an agent has access to when it decides what to do. It comes from three main sources:
- Memory: what the agent remembers about the user and previous conversations, which helps it adapt to their preferences.
- The message prompt: the conversation so far, plus any files and images passed as content parts (Part 3).
- Tools: results from searches, database lookups and other tool calls.
Context matters because agents act on it. It drives workflow patterns such as routing, where the agent chooses its next path based on what it currently knows (Part 6). An agent with poor context makes poor decisions, however good the model is.
Memory as Context
Memory lets an agent save information and recall it later when it is needed. Without memory, every conversation starts afresh. With it, the agent builds context over time and gains task continuity: it can pick up where it left off and draw on past experience.
There are several ways to add memory to an agent:
- Provider-defined tools, such as Anthropic's memory tool, which lets the model read and write its own memory files.
- Memory providers: dedicated memory services and framework features that store and retrieve memories for you.
- Custom tools you build yourself, for example
saveNoteandrecallNotestools backed by your own database.
If you use the Vercel AI SDK, you can also get Mastra's memory without switching frameworks: its withMastra() wrapper adds memory and processors to any AI SDK model (Part 2).
Tools
Tools let an agent interact with external systems, so it can do things beyond generating text. Anything the model can't do on its own is done through a tool. Tools also feed context back to the agent. Without loss of generality, tools are what make AI agents capable of autonomous action.
A tool is defined with three things: a name, a description of what it does, and an input schema describing the data it needs. An execute function then does the work:
import { tool } from 'ai';
import { z } from 'zod';
const getInvoice = tool({
description: 'Look up an invoice by its invoice number',
inputSchema: z.object({
invoiceNumber: z.string().describe('The invoice number, e.g. INV-1042'),
}),
execute: async ({ invoiceNumber }) => {
// Query your own database here
return db.invoices.findByNumber(invoiceNumber);
},
});The model never runs your code directly. It reads the description, decides to call the tool, and produces input that matches the schema. The SDK validates that input, runs your function and passes the result back to the model. Clear descriptions are essential, because they are how the model decides which tool to use.
Types of Tools
There is almost no limit to what can be a tool; even memory can be one. Tools fall into three groups:
| Type | Examples |
|---|---|
| Custom tools | Your own functions: database queries, internal APIs, calculations. |
| Provider tools | Tools run by the model provider: web search, web fetch, code execution, memory. |
| Community tools | Ready-made tools from others, often available through MCP servers (Part 7). |
Controlling Tool Choice
The toolChoice setting controls whether and how the model uses tools:
| Value | Behaviour |
|---|---|
'auto' | The default. The model decides whether to call a tool and which one. |
'required' | The model must call a tool. |
'none' | The model may not call any tools. |
{ type: 'tool', toolName: 'getInvoice' } | The model must call this specific tool. |
Workflow patterns are another way to control tool use: instead of letting the model choose freely, your code decides which step, and which tools, come next (Part 6).
Advertisement
Middleware
Middleware intercepts calls to the model so you can modify prompts before they are sent and responses after they arrive. Typical uses are adding guardrails, redacting personal information, caching and logging. The AI SDK supports this with wrapLanguageModel, and frameworks offer equivalents, such as Mastra's processors.
Mastra's Building Blocks
Different frameworks organise these ideas in their own ways. Mastra, for example, is built around agents that have access to tools, workflows that decide which steps and tools run when, and scorers that evaluate the quality of the agent's responses. Thinking in these four parts is a useful way to design any agent.
What's Next
In Part 5 we put prompts, tools and context together in the agent loop, then build multi-agent systems with subagents.
Building AI Agents: the series
- Part 1: What Are AI Agents?
- Part 2: Agent Development Tools, Vercel AI SDK and Providers
- Part 3: Prompts: Text, Instructions and Messages
- Part 4: Context, Memory and Tools (you are here)
- Part 5: The Agent Loop, Skills and Subagents
- Part 6: Workflow Patterns, RAG and Events
- Part 7: Agent Protocols: MCP, A2A and Beyond
Cover photo: Steve A Johnson on Unsplash
Chainbook builds intelligent business systems for African professionals, and AI agents are at the heart of how we build them.