Is there a notion of tool context?
is there a notion of tool context?
Now I have a complete picture. Let me summarize the tool context feature in the Vercel AI SDK:
##Yes, there is a "Tool Context" concept in the AI SDK
It's implemented via experimental_context - an experimental feature that allows you to pass arbitrary data from generateText/streamText into tool execution.
###How it works:
- Pass context to the API call:
const result = await generateText({
model: openai('gpt-4o'),
tools: { /* ... */ },
experimental_context: { weatherApiKey: '123', userId: 'abc' },
prompt: 'What is the weather?',
});
- Access in tool's
executefunction:
const weatherTool = tool({
description: 'Get the weather',
inputSchema: z.object({ location: z.string() }),
execute: async ({ location }, { experimental_context }) => {
// Access the context (you'll need to type cast or validate)
const ctx = experimental_context as { weatherApiKey: string };
// Use ctx.weatherApiKey for API calls, etc.
return { temperature: 72 };
},
});
###The ToolExecutionOptions interface:
The context is part of ToolExecutionOptions which provides:
| Property | Description |
|---|---|
toolCallId | The ID of the tool call |
messages | Messages sent to the LLM to initiate the response |
abortSignal | Signal to abort the operation |
experimental_context | User-defined context (the tool context you're asking about) |
###Modifying context between steps:
You can use prepareStep to change the context between multi-step tool calls:
await generateText({
// ...
experimental_context: { step: 0 },
prepareStep: ({ steps, experimental_context }) => {
return {
experimental_context: { step: steps.length + 1 },
};
},
});
###⚠️ Important Notes (from the source code):
Treat the context object as immutable inside tools.
Mutating the context object can lead to race conditions and unexpected results when tools are called in parallel.
If you need to mutate the context, analyze the tool calls and results inprepareStepand update it there.
The feature is marked experimental (can break in patch releases), as indicated by the experimental_ prefix.
END OF POST