Overview
Thegenerate() method is the recommended way to execute your agent when you want automatic trace collection, timing, and inference result creation. It handles the complete lifecycle:
- Initializes trace collection
- Executes your agent with timing measurement
- Creates/updates the inference result with all metadata
- Saves collected traces to the platform
- Cleans up trace context (prevents memory leaks)
This is the recommended method for production use as it handles all trace lifecycle management automatically.
Method Signature
Parameters
An instance of a class that extends
galtea.Agent. Must implement the call() method.The session object to associate the inference result with. Obtain via
galtea.sessions.create() or galtea.sessions.get().The user’s input message to send to the agent.
Optional ID of an existing inference result to update. If not provided, a new inference result is created.
Returns
Returns anInferenceResult object with all captured data.
Example
What Gets Captured
Thegenerate() method automatically captures and saves:
| Data | Source | Description |
|---|---|---|
| Input | user_input parameter | The user’s message |
| Output | AgentResponse.content | The agent’s response |
| Retrieval Context | AgentResponse.retrieval_context | Context used (for RAG) |
| Latency | Measured | End-to-end execution time in ms |
| Usage Info | AgentResponse.usage_info | Token counts (if provided) |
| Cost Info | AgentResponse.cost_info | Cost data (if provided) |
| Traces | @trace decorators | All traced operations |
Providing Usage and Cost Information
Your agent can return usage and cost information in theAgentResponse:
Comparison with Manual Approach
| Feature | generate() | Manual |
|---|---|---|
| Trace initialization | Automatic | traces.start_collection_context() |
| Agent execution | Automatic | Manual call |
| Timing measurement | Automatic | Manual |
| Inference result | Auto-created | inference_results.create() |
| Trace saving | Automatic | traces.save_context() |
| Memory cleanup | Automatic | traces.clear_context() |
| Error handling | Built-in | Manual try/finally |
Error Handling
If the agent raises an exception,generate() ensures trace context is cleaned up:
Related Methods
- Create Inference Result - Manual creation
- Trace Service - Manual trace management
- Simulating Conversations - Multi-turn simulations