Collector
This feature was added in 0.79.0
The Collector allows you to inspect the internal state of BAML function calls, including raw HTTP requests, responses, usage metrics, and timing information, so you can always see the raw data, without any abstraction layers.
Quick Start
Python
TypeScript
Ruby
Common Use Cases
Basic Logging
Python
TypeScript
Ruby
Tags
You can attach custom metadata to function calls using tags. These can come from a parent trace context or be specified per call.
Python
TypeScript
Go
Managing Collector State
Python
TypeScript
Ruby
Using Multiple Collectors
You can use multiple collectors to track different aspects of your application:
Python
TypeScript
Ruby
Usage Tracking
Python
TypeScript
Ruby
Accessing SSE Responses (Streaming)
When using streaming, you can access the raw Server-Sent Events (SSE) responses received from the LLM provider. This is useful for debugging, logging, or accessing provider-specific data not exposed in the standard response.
Python
TypeScript
SSE responses capture the raw streaming data from the provider. For HTTP-based providers (OpenAI, Anthropic, Google, etc.), this is the actual SSE event data. For AWS Bedrock, which uses a binary protocol, the responses contain a JSON wrapper with the Debug representation of the SDK types (see AWS SDK serialization issue).
API Reference
Collector Class
The Collector class provides properties to introspect the internal state of BAML function calls.
The Collector class provides the following methods:
FunctionLog Class
The FunctionLog class has the following properties:
Timing Class
The Timing class has the following properties:
StreamTiming Class (extends Timing)
No unique properties.
Usage Class
The Usage class has the following properties:
Note: Usage may not include all provider-specific token types like “thinking_tokens” or “cache_creation_input_tokens”. For those, you may need to look at the raw HTTP response and build your own adapters.
LLMCall Class
The LLMCall class has the following properties:
LLMStreamCall Class (extends LLMCall)
The LLMStreamCall includes the same properties as LLMCall plus the following:
HttpRequest Class
The HttpRequest class has the following properties:
HttpResponse Class
The HttpResponse class has the following properties:
HTTPBody Class
The HTTPBody class has the following properties:
SSEResponse Class
The SSEResponse class represents a single Server-Sent Event received during streaming.
The SSE event type (e.g., “message_delta”, “content_block_delta”) and event ID are not currently exposed. Only the data payload is available via text and json().
Related Topics
- Using with_options - Learn how to configure logging globally
- TypeBuilder - Build custom types for your BAML functions
- Client Registry - Manage LLM clients and their configurations
Best Practices
- Use a single collector instance when tracking related function calls in a chain.
- Clear the collector when reusing it for unrelated operations.
- Consider using multiple collectors to track different parts of your application.
- Use function IDs when tracking specific calls in parallel operations.
- For streaming calls, be aware that
http_responsewill be null, but you can still access usage information.