Overview
Callbacks are the primary mechanism for customizing agent behavior in Solace Agent Mesh. They intercept execution at critical points, allowing you to modify requests, responses, and tool outputs.Callback Architecture
Callbacks are registered with the ADK agent and executed at specific lifecycle events:Built-in Callbacks
Solace Agent Mesh includes several powerful built-in callbacks:repair_history_callback
When: Before every LLM callPurpose: Repair conversation history with dangling tool calls
process_artifact_blocks_callback
When: After every LLM responsePurpose: Parse and process fenced artifact blocks in streaming responses
- Streaming artifact creation with progress updates
- Early embed resolution (math, datetime, uuid)
- Automatic versioning
- Error handling for malformed blocks
manage_large_mcp_tool_responses_callback
When: After MCP tool executionPurpose: Handle large MCP responses by saving as artifacts
save_mcp_response_as_artifact_intelligent to intelligently handle different content types:
Custom Callbacks
Creating Custom Callbacks
You can create custom callbacks for specific agent behaviors:Registering Custom Callbacks
Callbacks are registered during agent setup:Callback Context
Callbacks receive aCallbackContext object with access to:
Accessing Services
Storing State
Callback Return Values
Continuing Execution
ReturnNone to continue normal execution:
Short-Circuiting
Return a response to skip LLM call:Callback Ordering
Callbacks execute in registration order:Error Handling
Callbacks should handle errors gracefully:Performance Considerations
Async Operations
Callbacks can be async for I/O operations:Caching
Cache expensive operations:Best Practices
- Keep callbacks focused: Each callback should have one clear responsibility
- Handle errors gracefully: Don’t crash the agent on callback errors
- Use async when needed: I/O operations should be async
- Document behavior: Clearly document what each callback does
- Test thoroughly: Callbacks are critical path code
See Also
- Agent Lifecycle - Understand when callbacks execute
- Agent Configuration - Configure callback behavior
- Tool Development - Create tools that work with callbacks