Manual capture LLM analytics installation
If you're using a different server-side SDK or prefer to use the API, you can manually capture the data by calling the capture method or using the capture API.
Capture via API
1. Install
2. Initialize PostHog
3. Capture Event
1. Install
2. Initialize PostHog
3. Capture Event
1. Install
2. Initialize PostHog
3. Capture Event
1. Install
2. Initialize PostHog
3. Capture Event
1. Install
2. Initialize PostHog
3. Capture Event
Event Properties
Each event type has specific properties. See the tabs below for detailed property documentation for each event type.
A generation is a single call to an LLM.
Event name: $ai_generation
Core properties
| Property | Description |
|---|---|
$ai_trace_id | The trace ID (a UUID to group AI events) like |
$ai_session_id | (Optional) Groups related traces together. Use this to organize traces by whatever grouping makes sense for your application (user sessions, workflows, conversations, or other logical boundaries). |
$ai_span_id | (Optional) Unique identifier for this generation |
$ai_span_name | (Optional) Name given to this generation |
$ai_parent_id | (Optional) Parent span ID for tree view grouping |
$ai_model | The model used |
$ai_provider | The LLM provider |
$ai_input | List of messages sent to the LLM. Each message should have a |
$ai_input_tokens | The number of tokens in the input (often found in response.usage) |
$ai_output_choices | List of response choices from the LLM. Each choice should have a |
$ai_output_tokens | The number of tokens in the output (often found in response.usage) |
$ai_latency | (Optional) The latency of the LLM call in seconds |
$ai_http_status | (Optional) The HTTP status code of the response |
$ai_base_url | (Optional) The base URL of the LLM provider |
$ai_request_url | (Optional) The full URL of the request made to the LLM API |
$ai_is_error | (Optional) Boolean to indicate if the request was an error |
$ai_error | (Optional) The error message or object |
Cost properties
Cost properties are optional as we can automatically calculate them from model and token counts. If you want, you can provide your own cost properties or custom pricing instead.
Pre-calculated costs
| Property | Description |
|---|---|
$ai_input_cost_usd | (Optional) The cost in USD of the input tokens |
$ai_output_cost_usd | (Optional) The cost in USD of the output tokens |
$ai_request_cost_usd | (Optional) The cost in USD for the requests |
$ai_web_search_cost_usd | (Optional) The cost in USD for the web searches |
$ai_total_cost_usd | (Optional) The total cost in USD (sum of all cost components) |
Custom pricing
| Property | Description |
|---|---|
$ai_input_token_price | (Optional) Price per input token (used to calculate |
$ai_output_token_price | (Optional) Price per output token (used to calculate |
$ai_cache_read_token_price | (Optional) Price per cached token read |
$ai_cache_write_token_price | (Optional) Price per cached token write |
$ai_request_price | (Optional) Price per request |
$ai_request_count | (Optional) Number of requests (defaults to 1 if |
$ai_web_search_price | (Optional) Price per web search |
$ai_web_search_count | (Optional) Number of web searches performed |
Cache properties
| Property | Description |
|---|---|
$ai_cache_read_input_tokens | (Optional) Number of tokens read from cache |
$ai_cache_creation_input_tokens | (Optional) Number of tokens written to cache (Anthropic-specific) |
Model parameters
| Property | Description |
|---|---|
$ai_temperature | (Optional) Temperature parameter used in the LLM request |
$ai_stream | (Optional) Whether the response was streamed |
$ai_max_tokens | (Optional) Maximum tokens setting for the LLM response |
$ai_tools | (Optional) Tools/functions available to the LLM |
A trace is a group that contains multiple spans, generations, and embeddings. Traces can be manually sent as events or appear as pseudo-events automatically created from child events.
Event name: $ai_trace
Core properties
| Property | Description |
|---|---|
$ai_trace_id | The trace ID (a UUID to group related AI events together) |
$ai_session_id | (Optional) Groups related traces together. Use this to organize traces by whatever grouping makes sense for your application (user sessions, workflows, conversations, or other logical boundaries). |
$ai_input_state | The input of the whole trace or any JSON-serializable state |
$ai_output_state | The output of the whole trace or any JSON-serializable state |
$ai_latency | (Optional) The latency of the trace in seconds |
$ai_span_name | (Optional) The name of the trace |
$ai_is_error | (Optional) Boolean to indicate if the trace encountered an error |
$ai_error | (Optional) The error message or object if the trace failed |
Pseudo-trace Events
When you send generation ($ai_generation), span ($ai_span), or embedding ($ai_embedding) events with a $ai_trace_id, PostHog automatically creates a pseudo-trace event that appears in the dashboard as a parent grouping. These pseudo-traces:
- Are not actual events in your data
- Automatically aggregate metrics from child events (latency, tokens, costs)
- Provide a hierarchical view of your AI operations
- Do not require sending an explicit
$ai_traceevent
This means you can either:
- Send explicit
$ai_traceevents to control the trace metadata - Let PostHog automatically create pseudo-traces from your generation/span events
A span is a single action within your application, such as a function call or vector database search.
Event name: $ai_span
Core properties
| Property | Description |
|---|---|
$ai_trace_id | The trace ID (a UUID to group related AI events together) |
$ai_session_id | (Optional) Groups related traces together. Use this to organize traces by whatever grouping makes sense for your application (user sessions, workflows, conversations, or other logical boundaries). |
$ai_span_id | (Optional) Unique identifier for this span |
$ai_span_name | (Optional) The name of the span |
$ai_parent_id | (Optional) Parent ID for tree view grouping ( |
$ai_input_state | The input state of the span or any JSON-serializable state |
$ai_output_state | The output state of the span or any JSON-serializable state |
$ai_latency | (Optional) The latency of the span in seconds |
$ai_is_error | (Optional) Boolean to indicate if the span encountered an error |
$ai_error | (Optional) The error message or object if the span failed |
An embedding is a single call to an embedding model to convert text into a vector representation.
Event name: $ai_embedding
Core properties
| Property | Description |
|---|---|
$ai_trace_id | The trace ID (a UUID to group related AI events together). Must contain only letters, numbers, and special characters: |
$ai_session_id | (Optional) Groups related traces together. Use this to organize traces by whatever grouping makes sense for your application (user sessions, workflows, conversations, or other logical boundaries). |
$ai_span_id | (Optional) Unique identifier for this embedding operation |
$ai_span_name | (Optional) Name given to this embedding operation |
$ai_parent_id | (Optional) Parent span ID for tree-view grouping |
$ai_model | The embedding model used |
$ai_provider | The LLM provider |
$ai_input | The text to embed |
$ai_input_tokens | The number of tokens in the input |
$ai_latency | (Optional) The latency of the embedding call in seconds |
$ai_http_status | (Optional) The HTTP status code of the response |
$ai_base_url | (Optional) The base URL of the LLM provider |
$ai_request_url | (Optional) The full URL of the request made to the embedding API |
$ai_is_error | (Optional) Boolean to indicate if the request was an error |
$ai_error | (Optional) The error message or object if the embedding failed |
Cost properties
Cost properties are optional as we can automatically calculate them from model and token counts. If you want, you can provide your own cost property instead.
| Property | Description |
|---|---|
$ai_input_cost_usd | (Optional) Cost in USD for input tokens |
$ai_output_cost_usd | (Optional) Cost in USD for output tokens (usually 0 for embeddings) |
$ai_total_cost_usd | (Optional) Total cost in USD |