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.
You can attach custom metadata to function calls using tags. These can come from a parent trace context or be specified per call.
You can use multiple collectors to track different aspects of your application:
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.
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).
When using providers that support prompt caching (like Anthropic, OpenAI, Google, or Vertex), you can track cached input tokens via the cached_input_tokens field:
Cached token tracking is supported for Anthropic, OpenAI, Google AI, and Vertex AI providers. AWS Bedrock does not currently support cached token reporting and will return null for this field.
The Collector class provides properties to introspect the internal state of BAML function calls.
The Collector class provides the following methods:
The FunctionLog class has the following properties:
The Timing class has the following properties:
No unique properties.
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.
The LLMCall class has the following properties:
The LLMStreamCall includes the same properties as LLMCall plus the following:
The HttpRequest class has the following properties:
The HttpResponse class has the following properties:
The HTTPBody class has the following properties:
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().
http_response will be null, but you can still access usage information.