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 your agents to access real-time data from external sources like APIs, databases, and services. 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, databases, or other sources
- Request context integration: Use request-specific parameters to customize data fetching
- Data transformation: Transform API responses into the exact format your agent needs
- Template interpolation: Use request context variables in URLs, headers, and request bodies
- Built-in Caching: Automatic caching of fetched data based on trigger settings
How Context Fetchers Work
- Request Arrives → HTTP request
- Validation → Validate headers against request context schema
- Context Resolution → Resolve context variables using templates
- Caching → Check cache, fetch if needed, store results
- Agent Execution → Provide resolved context to agent prompt
Basic Usage
Let's create a simple context fetcher that retrieves user information:
Using the Context Fetcher
Template interpolation
Use double curly braces {{}}
to reference request context variables in your fetcher configuration:
Data transformation
The transform
property lets you extract exactly what you need from API responses using dot notation:
GraphQL support
GraphQL queries are fully supported through the body
property. This allows you to make precise queries and leverage GraphQL's type safety and efficiency.
Basic GraphQL query
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
-
Optimize Performance
- Keep transforms simple and specific
- Use GraphQL to minimize data transfer
- Leverage caching when possible
-
Security Considerations
- Never expose sensitive credentials in templates
- Validate and sanitize all input data
- Use appropriate authentication headers