RunContext & Session Isolation
RunContext is an isolated key-value store created per Runner.run() execution turn. It threads sensitive application data (userId, orgId, database connections) directly to tools and guardrails without exposing credentials to LLM prompt messages.
Why Isolated Context Matters
In multi-tenant AI applications, passing user IDs or database connections directly inside the LLM prompt is risky:
- Prompt Injections: Malicious users could instruct the model to substitute someone else’s user ID.
- Context Window Waste: Injecting session boilerplate into every turn wastes precious token context.
RunContext solves this by keeping private application context in your Node environment.
Setting & Consuming Context
1. Passing Context to Runner.run()
typescript
2. Consuming Context Inside Tools
typescript
Context Lifecycle
- Created: A new
RunContextis instantiated at the start ofrunner.run(). - Inherited: Sub-agent handoffs inherit the parent
RunContextseamlessly. - Destroyed: When
runner.run()completes or throws, the context object is garbage collected.