Tool API Context
Tool API Context
Pass arbitrary context from generateText/streamText to tool execution via experimental_context. Access context in tool execute functions for dynamic behavior.
Problems solved
- Pass request-scoped values through experimental_context
- Read shared context inside tool execute without model-supplied secrets
- Customize tool behavior per user or session without forking tool definitions
Use cases
- User-preference-aware tools
- Per-tenant API credential injection
- Session-scoped tool configuration
- Context-aware tool demos
Setup
Requirements, wiring steps, and what this pattern adds to your project.
Getting started
Pick how you want to pull this pattern in. Then wire env vars and routes the same way.
- 1
Install from the preview toolbar

Copy the install command above and run it in your project — Pro patterns include a short-lived token.
- 2
Add environment variables to .env.local
OPENAI_API_KEY
- 3
Point client fetches at your API routes
Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.
- 4
Customize the agent and tool files
Adapt prompts, tools, and stop conditions for your product — Copy for AI in the toolbar helps seed that work.
- 5
Run your dev server and open the pattern route
Install dependencies if needed, then start the app and verify responses.
Environment variables
OPENAI_API_KEYGet key
Capabilities
AI SDK APIs
streamTextconvertToModelMessagestooluseChatexperimental_contextProviders
Files added
6 filesapp/page.tsxapp/layout.tsxapp/api/tool-context/route.tscomponents/context-demo.tsxtools/weather.tstools/weather-ui.tsx
Dependencies
14 totalnpm packages
7
Registry components
Critical files
tool-context/route.tsAPI route demonstrating experimental_context passing. Derives context from user input and passes it via experimental_context to streamText, making it available to all tools.
tools/weather.tsWeather tool demonstrating how to access experimental_context in the execute function's second parameter. Shows the receiving side of the context pattern.
Requirements
- Complexity
- Beginner
- Setup time
- ~5 minutes
- Node
- 24+
- Memory
- 512MB









