Skip to content

Python: ApplicationInsights agent span has incorrect token numbers #1320

Closed

Description

@jomalsan

I am using:

  • agent-framework v 1.0.0b251007 (But the same was true for v 1.0.0b251001)
  • AzureOpenAIResponsesClient
  • setup_observability(enable_sensitive_data=True, applicationinsights_connection_string="omitted")

What is happening:
In the latest version, each independent call to the model is being tracked as expected, with their token usage correctly attached. The agent's span then also records token usage, but is set to the values of the final llm call. This is misleading

Expected behavior is either:

  1. The agent span does not report token usage because they are reported on the model call
  2. The agent span reports the sum of tokens used in it's children spans

Screenshot showing application insights with the agent's span. The token usage matches the second chat o4-mini span's token usage:

Image

Activity

  1. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Oct 8, 2025
  2. added
    agentsUsage: [Issues, PRs], Target: Single agent
    and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Oct 9, 2025
  3. jomalsan commented on Oct 14, 2025

    @jomalsan
    Author

    After thinking about this more, I think the agent span not reporting token usage is the correct call. This way when using external observability like Langfuse we don't double count the tokens

  4. eavanvalkenburg commented on Dec 10, 2025

    @eavanvalkenburg
    Member

    Jon Malsan (@jomalsan) I had a good look at this, and I made a update to the way tokens are added, where for depending on the type of agent it will or won't add them to the invoke_agent span, and not for ChatAgents, so that should fix it. I spun up LangFuse locally to validate, and did notice that I had to go in and remove the tokenizer setup for the model that I'm using, because the agent span does have the model in it, so Langfuse was still doing some kind of calculation to add a token count and cost to that span, rather then just aggregating from the underlying chat spans, so be carefull about that. Once I have some other fixes in the observability space done, it will go in and we'll release. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

agentsUsage: [Issues, PRs], Target: Single agentobservabilityUsage: [Issues, PRs], Target: observability related featurespythonUsage: [Issues, PRs], Target: Python

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions