Skip to content

fix: add cost metric support to OpenRouterResponses - #9568

Open
aayush598 wants to merge 2 commits into
agno-agi:mainfrom
aayush598:fix/openrouter-responses-cost-metric
Open

fix: add cost metric support to OpenRouterResponses#9568
aayush598 wants to merge 2 commits into
agno-agi:mainfrom
aayush598:fix/openrouter-responses-cost-metric

Conversation

@aayush598

@aayush598 aayush598 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes an issue where OpenRouterResponses did not populate MessageMetrics.cost even when the OpenRouter Responses API returned cost information in the usage data.

The fix propagates the optional cost value from response_usage into MessageMetrics.cost while remaining compatible with usage objects that do not expose a cost attribute.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Improvement
  • Model update
  • Other:

Checklist

  • Code complies with style guidelines
  • Ran format/validation scripts (./scripts/format.sh and ./scripts/validate.sh)
  • Self-review completed
  • Documentation updated (comments, docstrings)
  • Examples and guides: Relevant cookbook examples have been included or updated (if applicable)
  • Tested in clean environment
  • Tests added/updated (if applicable)

Duplicate and AI-Generated PR Check

  • I have searched existing open pull requests and confirmed that no other PR already addresses this issue
  • If a similar PR exists, I have explained below why this PR is a better approach
  • Check if this PR was entirely AI-generated (by Copilot, Claude Code, Cursor, etc.)

Additional Notes

Added regression coverage for cost values, missing cost attributes, and zero-cost responses.
No API or breaking changes.
No documentation or cookbook changes are required for this internal metrics fix.
Related issue: #9566

@aayush598
aayush598 requested a review from a team as a code owner August 14, 2026 16:24
@greptile-apps

greptile-apps Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR propagates an optional Responses API usage cost into MessageMetrics for OpenRouter while preserving None and zero-cost behavior.

  • Adds cost extraction to the shared OpenAI Responses metrics conversion.
  • Adds OpenAI and OpenRouter unit coverage for populated, absent, and zero cost values.
  • The new coverage directly mocks the target attribute rather than validating its SDK-deserialized representation.

Confidence Score: 4/5

The PR appears safe to merge, with a non-blocking gap in regression-test fidelity around SDK deserialization of OpenRouter’s cost field.

The production assignment safely handles missing and zero cost values, but the tests do not establish that a real SDK-deserialized OpenRouter usage object exposes the provider-specific field through normal attribute access.

Files Needing Attention: libs/agno/tests/unit/models/openai/test_openai_responses_metrics.py; libs/agno/tests/unit/models/openrouter_model/test_openrouter_responses.py

Important Files Changed

Filename Overview
libs/agno/agno/models/openai/responses.py Adds guarded propagation of an optional usage cost into message metrics without altering native OpenAI behavior.
libs/agno/tests/unit/models/openai/test_openai_responses_metrics.py Covers metric conversion cases, but its plain mocks bypass the SDK deserialization boundary on which cost availability depends.
libs/agno/tests/unit/models/openrouter_model/test_openrouter_responses.py Adds OpenRouter cost assertions using the inherited metrics method, although the mock predetermines normal attribute access.

Reviews (1): Last reviewed commit: "fix: add cost metric support to OpenRout..." | Re-trigger Greptile

Comment thread libs/agno/tests/unit/models/openai/test_openai_responses_metrics.py
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.

1 participant