Skip to content

\ud83d\udc1e [Bug] Bare in Monitor drops token-usage keys silently (incorrect token counting) #1187

Description

@sljdxde

Version

master (commit 254ecd2)

System

OS Independent — affects token/cost accounting correctness.

Actions

Two LLM calls share the same trace_id; the monitor merges their token_usage dicts.

Problem

agentuniverse/base/util/monitor/monitor.py:246:

for key, value in cur_token_usage.items():
    try:
        result_usage[key] = old_token_usage[key] + value if key in old_token_usage else value
    except:
        # not addable value
        pass

The bare except: discards the current key's value entirely when the two values are not
addable (e.g. one is a dict/string, the other a number). The result is that some token-usage
keys silently vanish from result_usage, so downstream token/cost totals are undercounted
a correctness bug, not just a logging gap.

Expected

When values are not addable, the newer value should still be recorded (or at least the situation
should be logged), rather than dropping the key. Provide an explicit, observable merge strategy.

reproduce

# old_token_usage = {'prompt_tokens': 10, 'meta': {...}}
# cur_token_usage = {'prompt_tokens': 5, 'meta': 'str'}
# after merge, 'meta' is dropped because {...} + 'str' raises -> bare except -> pass
# result_usage is missing 'meta' -> undercount / lost info

References

  • agentuniverse/base/util/monitor/monitor.py:246

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions