Skip to content

Commit 3dfd9fd

Browse files
committed
Improve Instrumentation for usage tracking
1 parent f0ace9a commit 3dfd9fd

16 files changed

Lines changed: 779 additions & 740 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ response.usage.reasoning_tokens # OpenAI o1 models
163163
response.usage.service_tier # Anthropic
164164
```
165165

166+
**Enhanced Instrumentation for APM Integration**
167+
- Unified event structure: `prompt.active_agent` and `embed.active_agent` (top-level) plus `prompt.provider.active_agent` and `embed.provider.active_agent` (per-API-call)
168+
- Event payloads include comprehensive data for monitoring tools (New Relic, DataDog, etc.):
169+
- Request parameters: `model`, `temperature`, `max_tokens`, `top_p`, `stream`, `message_count`, `has_tools`
170+
- Usage data: `input_tokens`, `output_tokens`, `total_tokens`, `cached_tokens`, `reasoning_tokens`, `audio_tokens`, `cache_creation_tokens` (critical for cost tracking)
171+
- Response metadata: `finish_reason`, `response_model`, `response_id`, `embedding_count`
172+
- Top-level events report cumulative usage across all API calls in multi-turn conversations
173+
- Provider-level events report per-call usage for granular tracking
174+
175+
**Multi-Turn Usage Tracking**
176+
- `response.usage` now returns cumulative token counts across all API calls during tool calling
177+
- New `response.usages` array contains individual usage objects from each API call
178+
- `Usage` objects support addition: `usage1 + usage2` for combining statistics
179+
166180
**Provider Enhancements**
167181
- OpenAI Responses API: `api: :responses` or `api: :chat`
168182
- Anthropic JSON object mode with automatic extraction

docs/actions/usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ description: Track token usage and performance metrics across all AI providers w
66

77
Track token consumption and performance metrics from AI provider responses. All providers return normalized usage statistics for consistent cost tracking and monitoring.
88

9+
::: tip Monitor Usage in Production
10+
See [Instrumentation](/framework/instrumentation) to monitor usage statistics in real-time using ActiveSupport::Notifications.
11+
:::
12+
913
## Accessing Usage
1014

1115
Get usage statistics from any response:
@@ -43,6 +47,8 @@ Calculate costs using token counts:
4347

4448
<<< @/../test/docs/actions/usage_examples_test.rb#cost_tracking{ruby:line-numbers}
4549

50+
**Monitor costs in production:** Use [Instrumentation](/framework/instrumentation#cost-tracking) to automatically track costs across all requests.
51+
4652
## Embeddings Usage
4753

4854
Embedding responses have zero output tokens:

0 commit comments

Comments
 (0)