Context Fetchers
Copy page
Learn how to use context fetchers to fetch data from external sources and make it available to your agents
Overview
Context fetchers allow you to embed real-time data from external APIs into your agent prompts. Instead of hardcoding information in your agent prompt, context fetchers dynamically retrieve fresh data for each conversation.
Key Features
- Dynamic data retrieval: Fetch real-time data from APIs.
- Dynamic Prompting: Use dynamic data in your agent prompts
- Request context integration: Use request-specific parameters to customize data fetching.
- Data transformation: Transform API responses into the exact format your agent needs.
Context Fetchers vs Tools
-
Context Fetchers: Pre-populate agent prompts with dynamic data
- Run automatically before/during conversation startup
- Data becomes part of the agent's system prompt
- Perfect for: Personalized agent personas, dynamic agent guardrails
- Example Prompt:
You are an assistant for ${userContext.toTemplate('user.name')} and you work for ${userContext.toTemplate('user.organization')}
-
Tools: Enable agents to take actions or fetch data during conversations
- Called by the agent when needed during the conversation
- Agent decides when and how to use them
- Example Tool Usage: Agent calls a "send_email" tool or "search_database" tool
Basic Usage
Let's create a simple context fetcher that retrieves user information:
Using Context Variables
Context variables can be used in your agent prompts using JSONPath template syntax {{contextVariableKey.field_name}}
.
Use the context config's toTemplate()
method for type-safe templating with autocomplete and validation.
Context variables are resolved using JSONPath notation.
Data transformation
The transform
property on fetch definitions lets you extract exactly what you need from API responses using JSONPath notation:
Best Practices
-
Use Appropriate Triggers
initialization
: Use when data rarely changesinvocation
: Use for frequently changing data
-
Handle Errors Gracefully
- Always provide a
defaultValue
- Use appropriate response schemas
- Always provide a
Related documentation
- Request Context - Learn how to pass dynamic context to your agents via HTTP headers