Overview
GalileoMiddleware is a middleware component that integrates with LangGraph agents to provide comprehensive tracing and logging. Unlike the callback-based approach, middleware automatically intercepts agent execution at key points:
- Agent lifecycle: Tracks when an agent starts and completes
- Model calls: Logs all LLM invocations with prompts, responses, and metadata
- Tool calls: Captures tool invocations including function names, arguments, and outputs
- Async support: Full support for both synchronous and asynchronous agent execution
Basic usage
To useGalileoMiddleware, simply add it to the middleware parameter when creating a LangChain agent:
The middleware automatically handles all logging internally. When the agent is invoked:
- An agent node is created to track the overall execution
- Each model call creates an LLM node with prompt and response details
- Each tool call creates a tool node with function name, arguments, and output
- All nodes are linked hierarchically under the agent node
Configuration options
GalileoMiddleware accepts the following parameters:
galileo_logger(optional): A customGalileoLoggerinstance. If not provided, a default logger is created.start_new_trace(default:True): Whether to start a new trace on agent invocation. Set toFalseto add to an existing trace.flush_on_chain_end(default:True): Whether to flush logs to Galileo when the agent completes.ingestion_hook(optional): A callback function that receivesTracesIngestRequestobjects before they’re sent to Galileo.
Custom logger
You can provide a custom logger instance to integrate with existing logging infrastructure:Python
Trace management
By default, each agent invocation creates a new trace. You can control trace behavior:Add to existing trace
To add agent execution to an existing trace, use a shared logger withstart_new_trace=False:
Python
Manual flush control
If you want to control when logs are flushed (e.g., for batch processing):Python
What gets logged
GalileoMiddleware captures the following information:
Agent node
- Input state (messages)
- Output state (final messages)
- Execution time
Model call nodes
- Model name and configuration (temperature, etc.)
- Input messages (including system message if present)
- Output response
- Tools available to the model
- Timing metrics (start time, time to first token if available)
Tool call nodes
- Tool/function name
- Tool arguments (serialized)
- Tool output
- Execution time
Comparison with GalileoCallback
GalileoMiddleware and GalileoCallback provide similar functionality but use different approaches:
| Feature | GalileoMiddleware | GalileoCallback |
|---|---|---|
| Integration point | LangGraph agents via middleware parameter | LangChain components via callbacks parameter |
| Setup complexity | Simple - add to middleware list | Manual - pass to each component |
| Agent support | Native support for LangGraph agents | Requires callback setup |
| Flexibility | Automatic agent-level tracing | Fine-grained control over individual components |
| Use case | LangGraph agents with minimal setup | Complex LangChain applications with custom needs |
GalileoMiddleware when:
- You’re building LangGraph agents
- You want automatic, drop-in logging
- You prefer simpler setup
GalileoCallback when:
- You need fine-grained control over logging
- You’re working with complex LangChain applications
- You want to log specific components selectively
Async support
GalileoMiddleware fully supports asynchronous execution. The middleware automatically handles both sync and async contexts:
Python
GalileoBaseHandler or GalileoAsyncBaseHandler) based on the execution context.
Best practices
- Use middleware for LangGraph agents: For LangGraph-based agents, middleware provides the simplest integration
- Add meaningful metadata: Include relevant project and session information in your logger configuration
- Configure flush behavior: For high-volume applications, consider disabling auto-flush and batch your logs
- Share loggers: Use the same logger instance across middleware for unified trace management
- Monitor execution: Review the hierarchical traces in Galileo to understand agent behavior
Example
You can find a complete example of usingGalileoMiddleware with a LangGraph agent in the LangChain Middleware Example.
Next steps
Related documentation
GalileoCallback
Use callbacks for fine-grained LangChain logging control.
Experiments
Learn how to run and track experiments with LangChain.