Skip to content

fix(llm_registry): track restored metrics id after subscriber notify - #6497

Open
Christian-Sidak wants to merge 1 commit into
OpenHands:mainfrom
Christian-Sidak:fix/issue-6487
Open

Christian-Sidak wants to merge 1 commit into
OpenHands:mainfrom
Christian-Sidak:fix/issue-6487

Conversation

@Christian-Sidak

@Christian-Sidak Christian-Sidak commented Nov 5, 2026 •

Copy link
Copy Markdown

HUMAN:

Bug: resumed conversations double-count LLM cost once a derived model is created via model_copy.

AGENT:

Why

After a conversation is resumed, ConversationStats.register_llm() calls restore_metrics() inside LLMRegistry.notify(), which replaces the agent LLM's _metrics object with a persisted one. The id of this new object is never recorded in _metrics_ids. When a derived model is later created via model_copy(), _ensure_independent_metrics() fails to detect the sharing and skips the reset, so both the parent and derived model share the same metrics object. get_combined_metrics() then counts the spend twice (3.0 instead of 2.0 for the agent, 6.0 instead of 3.0 combined).

Summary

One line added in LLMRegistry.add() after notify(): self._metrics_ids.add(id(llm.metrics)) records any post-notify metrics swap in _metrics_ids. The next model_copy() derivative is then correctly detected as sharing metrics and receives its own independent instance.

How to Test

Added test_resume_then_derive_no_double_count in tests/sdk/conversation/test_conversation_stats.py. It seeds persisted metrics, registers the agent LLM through the real LLMRegistry and ConversationStats, derives a copy via model_copy(), and asserts independent metrics and correct combined cost (reproducer from the issue).

Fixes #6487

After a conversation is resumed, ConversationStats.register_llm() calls
restore_metrics() on the agent LLM inside the notify() callback, swapping
its _metrics for a persisted object whose id was never added to
_metrics_ids.  A subsequent model_copy() derivative (ask-agent) therefore
shares that restored object, and _ensure_independent_metrics() fails to
detect the sharing -- the ask-agent copy is never reset, and
get_combined_metrics() counts the spend twice.

Fix: in LLMRegistry.add(), add one line after notify() that re-records
id(llm.metrics).  Any post-notify restore is now reflected in _metrics_ids,
so the next model_copy() derivative is correctly detected as sharing
metrics and receives its own independent instance.

Fixes OpenHands#6487

Signed-off-by: Christian Sidak <christian@sentineltech.eu>
Signed-off-by: Christian-Sidak <61099993+Christian-Sidak@users.noreply.github.com>
@Christian-Sidak

Copy link
Copy Markdown
Author

Following up on this. The fix tracks the restored metrics id after subscriber notify fires, so the restored id is what gets associated with the metrics record rather than the stale pre-restore id. CI is green.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Resumed conversations double-count cost once ask_agent runs

1 participant