Loading preview…
Loading preview…
Inject request-scoped context (user identity, database handles, session) into ToolLoopAgent tools via experimental_context, so the model never supplies sensitive identity in tool inputs.
Loading preview…
Requirements, wiring steps, and what this pattern adds to your project.
Pick how you want to pull this pattern in — then wire env vars and routes the same way.
Install from the preview toolbar
Copy the install command above and run it in your project — Pro patterns include a short-lived token.
Add environment variables to .env.local
OPENAI_API_KEY
Point client fetches at your API routes
Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.
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.
Run your dev server and open the pattern route
Install dependencies if needed, then start the app and verify responses.
OPENAI_API_KEYGet keyAI SDK APIs
ToolLoopAgentexperimental_contexttoolstepCountIsgatewayProviders
app/page.tsxapp/layout.tsxapp/api/context/route.tscomponents/context-demo.tsxcomponents/tool-call-trace.tsxlib/context-agent.tslib/request-context.tslib/demo-users.tstools/get-orders.tstools/get-profile.tsREADME.mdnpm packages
5
Registry components
tools/get-orders.tsCore pattern: tool whose inputSchema omits userId; execute reads identity and db from the second arg's experimental_context, casting to a RequestContext type.
context/route.tsResolves identity server-side and passes it to agent.generate({ prompt, experimental_context }). Returns tool calls and results for inspection.
lib/request-context.tsDefines the RequestContext type (userId, user, db) and a fake database. Replace with your real session + data layer.
Loading preview…
Early-bird pricing — limited time

Open on desktop for the interactive preview.
Inject request-scoped context (user identity, database handles, session) into ToolLoopAgent tools via experimental_context, so the model never supplies sensitive identity in tool inputs.