How the Agent Toolkit works
The Agent Toolkit sits between your application (chat UI and model provider) and the TomTom Maps SDK. Each user message goes through two phases, intent classification and then a tool loop. Shared state carries context across turns.
1. Intent classification
Before the model sees any tool, one lightweight LLM call (two if the first answer fails validation) picks the tools this turn needs. Fewer tools in the request means fewer wrong choices and fewer tokens.
- It reads each tool’s
classificationPrompt, a one-line “when to use me”. A custom tool that has one takes part automatically. - It sees the last user message plus up to eight earlier messages, each cut to 300 characters. It never sees the system prompt, tool results or state.
- Tools with
alwaysActive: true(clarifyIntentby default) are added every step, whatever the classifier picked. - For scopable tools (
analyseDatasets,deriveDatasets,clusterIncidents, and custom tools with ascopeSchema), it also names the data kinds the turn touches, and the tool’s schema shrinks to those kinds. See Scope-aware data tools. - If classification fails, the toolkit offers every tool (fail-open).
The default classifier reuses your main model. Give it a smaller one, change how much history it sees, or set its temperature so the same request picks the same tools. A reasoning model, such as gpt-5 or the o-series, takes no temperature, so leave it unset there:
import { createMapAgent, createDefaultClassifier } from '@tomtom-org/maps-sdk-plugin-agent-toolkit';import { openai } from '@ai-sdk/openai';
const agent = createMapAgent(map, { model: openai('gpt-4o'), classifier: createDefaultClassifier({ model: openai('gpt-4o-mini'), maxHistoryMessages: 8, // earlier messages sent (default 8) maxHistoryMessageLength: 300, // characters kept per earlier message (default 300) temperature: 0, // the same query picks the same tools (default: the model's own) }),});Or turn it off. Every tool is then offered every turn, and scopable tools keep their full, unscoped schema:
const agent = createMapAgent(map, { model: openai('gpt-4o'), classifier: false,});classifier also accepts your own function, (context) => Promise<ClassificationResult | null>. It receives the
messages and every tool’s metadata. Returning null offers every tool.
2. Tool loop
The model calls tools, such as geocode, search, route and show on map, in sequence or in parallel until it can answer.
- Tools return compact summaries (ids, counts, names, status). The geodata stays in state.
- The toolkit stores results as datasets with stable ids (
places-3,routes-1), and the model reads them back withrecallState. That is why “add a stop after the first one” or “what were those restaurants again?” work turns later. - The calls in one step run at the same time. When a built-in tool names a dataset that doesn’t exist yet, it waits for the calls that started before it in that step, then looks again. So “find hospitals in Las Vegas and chart the jams near them” works in one step, with the chart reading the hospitals the search is still loading.
- The loop stops after
maxStepssteps (default 10), or as soon as the model calls a tool withendsTurnOnCall.
The tool registry lists every tool.
Per-turn sequence
Hooking into the loop
| Option | Use it to |
|---|---|
prepareStep | Run your own per-step logic. Any activeTools it returns are intersected with the classifier’s pick. An instructions or messages override carries over to the later steps, so build instructions from initialInstructions and messages from [...initialMessages, ...responseMessages] |
maxSteps | Cap the steps per turn (default 10) |
providerOptions | Pass provider settings such as reasoning effort to every step |
stepProviderOptions | Return per-step provider settings from { activeTools, stepNumber }, merged over providerOptions |
outputSchemas | Set false for providers that reject structured tool outputs |
onToolExecute | Observe each call’s toolName, durationMs, isError and errorMessage |
const agent = createMapAgent(map, { model: openai('gpt-5'), providerOptions: { openai: { reasoningEffort: 'low' } }, // Think harder only on turns that write code. stepProviderOptions: ({ activeTools }) => activeTools?.some((name) => name === 'analyseDatasets' || name === 'deriveDatasets') ? { openai: { reasoningEffort: 'medium' } } : undefined, onToolExecute: ({ toolName, durationMs, isError }) => console.log(toolName, durationMs, isError),});Run one turn at a time per agent, because scoping rewrites the agent’s shared tool objects each turn, and state belongs to the agent too. Create one agent per concurrent conversation.
Per-tool classification prompts
If a custom tool is offered too often, or not when it should be, tune its classificationPrompt:
// Too broad — activates for any location questionclassificationPrompt: 'Get fleet vehicle data.'
// Precise — activates only for explicit vehicle ID referencesclassificationPrompt: 'Locate a fleet vehicle by its ID (e.g. "TT-001"); not for general location queries.'The system prompt never reaches the classifier, so an instruction there cannot make a tool available. The system prompt guide explains what to do instead.
Observing classification
onClassify receives each turn’s pick and per-tool scopes, or null if the classifier failed open:
const agent = createMapAgent(map, { model: openai('gpt-4o'), onClassify: (result) => { if (result) { console.log('Selected tools:', result.activeToolNames); console.log('Per-tool scopes:', result.toolScopes); // e.g. { analyseDatasets: { kinds: ['places', 'routes'] } } console.log('Classification took:', result.timeMs, 'ms', result.usage.totalTokens, 'tokens'); } },});Compacting a long conversation
Every step of a turn sends the whole conversation to the model, so each turn costs more than the one before. A chat
user starts over now and then, but an agent woken by events, such as a monitoring agent or a game’s AI player, never
does. compactMessages keeps that agent’s history short. It folds the older turns into a running summary with one
model call, and keeps the latest turns word for word.
If your agent doesn’t need a summary of what it drops, the AI SDK’s pruneMessages costs less, because it removes
reasoning, tool calls and tool results from older messages without calling the model.
To compact every few turns, pass the conversation and the previous summary, then send the new summary with the next turn:
import { compactMessages } from '@tomtom-org/maps-sdk-plugin-agent-toolkit';import type { ModelMessage } from 'ai';
let summary = '';let messages: ModelMessage[] = [];let turns = 0;
const onEvent = async (event: string) => { const memory = summary ? `What you remember: ${summary}\n\n` : ''; const result = await agent.generate({ messages: [...messages, { role: 'user', content: memory + event }] }); messages.push({ role: 'user', content: event }, ...result.responseMessages);
turns++; if (turns % 6 === 0) { ({ summary, messages } = await compactMessages({ model: openai('gpt-4o-mini'), messages, summary, keepTurns: 2 })); }};keepTurns- How many of the latest turns stay word for word. A turn starts at a user message, so a tool call always stays with its result. With0, every message is folded.summary- The summary the previous call returned.instructions- What the summary keeps and drops. By default, it keeps commitments, decisions and facts that later turns can’t recover, and drops what fresh context carries again.maxWords- The summary’s length limit. By default, 150 words.
The result holds the new summary, the kept messages and the call’s usage, to add to your token count. With no
more than keepTurns turns there is nothing to fold, so it makes no call and leaves usage out. The model reads the
folded turns as text: each tool call as its name and input, and each tool result, with every input and result cut to
200 characters.
When the model replies with no text, for example after a content filter stops it, compactMessages throws an
Error and folds nothing. Catch it and keep your summary and messages as they are, then try again a few turns later.
To tailor the summary, replace the instructions, or extend the defaults by appending to
COMPACT_MESSAGES_INSTRUCTIONS:
import { COMPACT_MESSAGES_INSTRUCTIONS } from '@tomtom-org/maps-sdk-plugin-agent-toolkit';
const compacted = await compactMessages({ model: openai('gpt-4o-mini'), messages, summary, keepTurns: 2, instructions: `${COMPACT_MESSAGES_INSTRUCTIONS} Keep every alert the user dismissed, and why.`,});Related
- Tool registry — the tools the classifier selects from.
- Customizing the system prompt — the prompt the tool loop runs with.
- Securing your agent — what bounds a turn, and why everything in this flow runs where the user can edit it.