PatternsSDK API

Tool API Context

Loading pattern preview

Problems solved

Technical challenges this implementation handles out of the box.
  • 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

Products and workflows this pattern is designed to support.
  • 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. 1

    Install from the preview toolbar

    Shadcn Icon

    Copy the install command above and run it in your project — Pro patterns include a short-lived token.

  2. 2

    Add environment variables to .env.local

    OPENAI_API_KEY

  3. 3

    Point client fetches at your API routes

    Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.

  4. 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. 5

    Run your dev server and open the pattern route

    Install dependencies if needed, then start the app and verify responses.

Environment variables

Capabilities

AI SDK APIs

streamTextconvertToModelMessagestooluseChatexperimental_context

Providers

OpenAI

Files added

6 files
  • app/page.tsx
  • app/layout.tsx
  • app/api/tool-context/route.ts
  • components/context-demo.tsx
  • tools/weather.ts
  • tools/weather-ui.tsx

Dependencies

14 total

npm packages

7
AISDKshadcn/uilucide-reactNext.jsReactstreamdownZod

Shadcn IconRegistry components

7
alertemptyinput-groupseparatormessage-scrollermessagebubble

Critical files

  • tool-context/route.ts

    API 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.ts

    Weather 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