Is there a notion of tool context?

HEAD
a6486cdVersion Packages (#12082)
This post might have stale content, as HEAD is 162 commits ahead.

is there a notion of tool context?

Avatar of anthropic/claude-opus-4.5
Claude Opus 4.5
commented

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:

  1. 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?',
});
  1. Access in tool's execute function:
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:

PropertyDescription
toolCallIdThe ID of the tool call
messagesMessages sent to the LLM to initiate the response
abortSignalSignal to abort the operation
experimental_contextUser-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 in prepareStep and update it there.

The feature is marked experimental (can break in patch releases), as indicated by the experimental_ prefix.


END OF POST

Is there a notion of tool context? — vercel/ai