Overview
Thegenerate() method is the recommended way to execute your agent when you want automatic span collection, timing, and trace creation. It handles the complete lifecycle:
- Initializes span collection
- Executes your agent with timing measurement
- Creates/updates the trace with all metadata
- Saves collected spans to the platform
- Cleans up span context (prevents memory leaks)
This is the recommended method for production use as it handles all span lifecycle management automatically.
Agent Options
- Simple
- Chat History
- Structured
The quickest way to get started. Your function receives just the latest user message as a string.
All three signatures work with
evaluations.run(), traces.generate(), and simulator.simulate(). Both sync and async functions are supported. The SDK auto-detects which signature you’re using from the type hint on the first parameter.For the full list of fields available on AgentInput (including structured input access via message metadata), see the AgentInput reference.Parameters
AgentType
required
The agent function to execute. Supported signatures:
- Simple:
(user_message: str) -> str— receives last user message - Chat History:
(messages: list[dict]) -> str— receives full chat history (OpenAI format) - Structured:
(input_data: AgentInput) -> AgentResponse— structured input/output. See the AgentInput reference for all available fields including structured input access via message metadata.
usage_info, cost_info, or retrieval_context.Session
required
The session object to associate the trace with. Obtain via
galtea.sessions.create() or galtea.sessions.get().str | dict
The input to send to the agent. Accepts a plain string or a dict with structured fields (e.g.
{"user_message": "hello", "chat_type": "support"}). Stored directly in the trace. If not provided and the session is test-based, the test case input is used automatically. Required for production/monitoring sessions. See Input Schema for how to define the expected field structure on your endpoint connection.bool
If
True, raises a ValueError when the agent returns an empty response. Defaults to False.context_data is automatically fetched from the session’s test case and passed to the agent via AgentInput.context_data. No need to provide it explicitly.Returns
Returns aTrace object with all captured data.
Example
What Gets Captured
Thegenerate() method automatically captures and saves:
Providing Usage and Cost Information
Your agent can return usage and cost information in theAgentResponse:
Comparison with Manual Approach
Error Handling
If the agent raises an exception,generate() ensures span context is cleaned up:
Related Methods
- Create Trace - Manual creation
- Span Service - Manual span management
- Simulating Conversations - Multi-turn simulations