You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 381d7b1
Browse filesBrowse the repository at this point in the historyBrowse files
fix(ag2): count Anthropic cached input; agent and chat replies in Observe
Anthropic input tokens: AG2's Anthropic mapper reports prompt_tokens without
cache reads and writes (its OpenAI mapper includes them), and AnthropicConfig
caches by default, so a chat span showed 4 input tokens for a 9,750-token
prompt. On anthropic spans whose tokens count, cache_read_input_tokens and
cache_creation_input_tokens are now added to gen_ai.usage.input_tokens, and
total_tokens is recomputed when present. The trace total can then exceed
AG2's UsageReport by the cached tokens (README, CHANGELOG). Duplicate
record_usage spans keep AG2's own numbers under ag2.usage.*.
Chat reply: a single completion goes on output.value as its message object.
Observe's chat view shows that as the assistant reply and shows nothing for
a one-item list.
Agent I/O: invoke_agent carries no content in AG2. With capture_content=True
it now gets the first chat input and the last chat output of its turn, so
the trace list shows the question and the answer. Hidden content is not
copied, and the agent's own values win.
Checked live on CLIProxy: Anthropic chat spans store 9751/94 and 9753/88
tokens; the capture run's trace list shows Input/Output, and the chat span
shows the assistant reply bubble.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copy file name to clipboardExpand all lines: python/frameworks/ag2/README.md
+11-3Lines changed: 11 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -165,11 +165,11 @@ not copied to `input.value` / `output.value` either.
165
165
| Span kind |`AGENT`, `LLM`, `TOOL` from `gen_ai.operation.name`|`await_human_input` and `record_usage` spans get no kind (see below) |
166
166
| Model |`gen_ai.request.model`, `gen_ai.response.model` pass through | When AG2 did not set them |
167
167
| Provider |`gen_ai.provider.name` from `provider_name` or the model response | When neither is known |
168
-
| Tokens |`gen_ai.usage.input_tokens` / `output_tokens` pass through; cache tokens are aliased to `gen_ai.usage.cache_read_tokens` / `cache_write_tokens` and thinking tokens to `gen_ai.usage.reasoning.output_tokens`, originals kept | When the usage field is zero/absent. Cost is not emitted |
168
+
| Tokens |`gen_ai.usage.input_tokens` / `output_tokens` pass through, except on `anthropic` spans, where cache read and write tokens are added to `input_tokens` (and `total_tokens` recomputed when present; see the note below). Cache tokens are aliased to `gen_ai.usage.cache_read_tokens` / `cache_write_tokens` and thinking tokens to `gen_ai.usage.reasoning.output_tokens`, originals kept | When the usage field is zero/absent. Cost is not emitted |
169
169
| Session | Not emitted by AG2. `session.id` from traceAI's `using_session` / `using_attributes` context, which wins over a static value; or set it yourself: `setup(..., span_attributes={"session.id": "..."})`| When neither is set |
170
170
| User, metadata, tags |`user.id`, `metadata`, `tag.tags` (and the other traceAI context keys) from `using_user` / `using_metadata` / `using_tags` / `using_attributes`; a key AG2 or `span_attributes` already set is not overridden | When the context does not set them |
171
171
| Tools |`gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.tool.type`; arguments/result only with `capture_content=True`||
172
-
| Input / output |`input.value` / `output.value` (what Observe shows as a span's input and output), copied from the captured chat messages, tool arguments and result, or human input| Without `capture_content=True`, when a `TraceConfig` flag hides the source, and always on`invoke_agent` and`record_usage` spans (AG2 records no content there) |
172
+
| Input / output |`input.value` / `output.value` (what Observe shows as a span's input and output), copied from the captured chat messages, tool arguments and result, or human input. A single chat completion goes on `output.value` as its message object, which Observe's chat view shows as the assistant reply. `invoke_agent` gets the first chat input and the last chat output of its turn, so the trace list shows the question and the answer | Without `capture_content=True`, when a `TraceConfig` flag hides the source, and always on `record_usage` spans (AG2 records no content there) |
173
173
| Errors | OTel status `ERROR` on failing tool / model / agent spans ||
174
174
175
175
Notes:
@@ -193,7 +193,15 @@ Notes:
193
193
|`subtask`| Only if the sub-agent named in `ag2.usage.label` is itself instrumented and its `invoke_agent` span ended earlier in the same trace | Moved when it is, kept otherwise |
194
194
195
195
With that, the trace total equals AG2's own `UsageReport` total; the tests
196
-
check this with real AG2 for every kind above. For the `subtask` match, the
196
+
check this with real AG2 for every kind above. Exception: with an
197
+
Anthropic model and prompt caching (`AnthropicConfig` caches by default),
198
+
AG2's Anthropic mapper reports `prompt_tokens` without the cached tokens,
199
+
while its OpenAI mapper and the GenAI conventions count them as input. The
200
+
processor adds `cache_read_input_tokens` and `cache_creation_input_tokens`
201
+
to `gen_ai.usage.input_tokens` on `anthropic` spans whose tokens count, so
202
+
the trace total is the real prompt size and exceeds `UsageReport` by the
203
+
cached tokens. A `record_usage` span that repeats a chat span keeps AG2's
204
+
own numbers under `ag2.usage.*`. For the `subtask` match, the
197
205
sub-agent's `TelemetryMiddleware` must use the agent's own name (`setup`
198
206
does; `create_telemetry_middleware(agent_name=...)` must pass `agent.name`).
199
207
On ag2 1.0.0 to 1.0.2 there are no `record_usage` spans, so aggregation,
0 commit comments