Invoke
Theinvoke() function allows you to execute agents programmatically from Python code, making it easy to test agents, integrate them into applications, and automate workflows.
Quick Start
Basic Usage
Invoke with Messages
Invoke with Datasets
Invoke with Parameters
Invocation Modes
Control how bot actions (especially LLM calls) are executed for testing and development:| Mode | Description | Cost | Use Case |
|---|---|---|---|
| live | Real API calls | $$$ per run | Production, fresh data |
| replay | Cached responses | $$$ first run, FREE after | Testing, CI/CD |
| manual | Developer-provided mocks | FREE always | Unit tests, deterministic behavior |
Live Mode (Default)
Runs against the real backend with LLM API (costs $):- Production invocations
- Development with real-time data
- When you need fresh LLM responses
Replay Mode (Recommended for Testing)
Uses cached responses - first run costs $, subsequent runs FREE:- Development and testing
- CI/CD pipelines
- Iterating on agent logic
- Running test suites
- Cache key: SHA256 hash of (bot_id, bot_updated_at, action_type, parameters)
- Automatic invalidation when bot is updated
- Multi-tenant isolation (scoped by organization)
- Only LLM calls are cached (configurable)
Replay Mode with Refresh
Force a fresh API call while staying in replay mode:- Cache contains outdated responses
- Testing cache refresh behavior
- Updating cache after bot logic changes
- Forcing fresh data without switching out of replay mode
Manual Mode
Uses developer-provided mock responses - always free:- Unit testing with deterministic responses
- Testing error handling
- CI/CD pipelines
- Offline development
- Manual mode requires mocks for all executed actions
- Error if no mock provided for an action
- Action type keys:
llm.message,memory.search,codeexec.run, etc.
Output Formats
Events Format (Default)
Returns raw events:Text Format
Returns human-readable text:Text with Verbose Steps
Show step-by-step execution:JSON Format
Returns structured summary:Streaming
Stream events in real-time:InvokeResult
Theinvoke() function returns an InvokeResult object with a clean structure following the executor pattern:
Understanding the Result Structure
Theresult field follows the standardized types.Result structure from the backend:
Example Usage
Accessing Messages from All Steps
Themessages field captures messages from all events including intermediate steps and sub-agents, not just the final output:
Complete API Reference
Parameters
bot_key(str, required): Bot key (e.g., “my-agent”, “data-question-answerer”)messages(list, optional): Messages in format[{"role": "user", "content": "..."}]parameters(dict, optional): Parameters to pass to the botdatasets(list, optional): Dataset slugs to include (e.g., [“sales-2024”, “customers”])mode(str or dict, optional): Invocation mode- String: “live” (default), “replay”, or “manual”
- Dict:
{"mode": "replay", "refresh": True}for advanced options
manual_mocks(dict, optional): Manual mock responses for mode=“manual”- Format:
{"action_type": {"status": "success", "output": {...}}}
- Format:
stream(bool, optional): Whether to stream events (default: False)output_format(str, optional): Output format - “events” (raw), “text” (formatted), or “json” (summary) (default: “events”)verbose(bool, optional): Show detailed steps (only for text format) (default: False)print_events(bool, optional): Print events as they arrive (default: False)
Keyword Arguments
endpoint(str): Custom API endpointauth_token(str): Custom auth token
Examples
Testing Agents
Multi-turn Conversation
Data Analysis
Streaming with Progress
Batch Processing
Integration with Flask
Error Handling
Best Practices
1. Use Replay Mode for Testing
2. Always Check Success
3. Use Appropriate Output Format
4. Stream Long-Running Agents
Troubleshooting
Bot Not Found
Make sure the agent is synced:Authentication Errors
Login first:Slow Invocations
Use replay mode:Import Errors
Install the SDK:See Also
- Agent Testing - Learn about testing agents
- CLI Invoke Command - CLI usage
- Examples - More examples