Skip to content

fix(agent): refund api_call_count when all retries are exhausted - #39115

Closed
lEWFkRAD wants to merge 1 commit into
NousResearch:mainfrom
lEWFkRAD:fix/refund-api-call-count-on-exhausted-retries
Closed

lEWFkRAD wants to merge 1 commit into
NousResearch:mainfrom
lEWFkRAD:fix/refund-api-call-count-on-exhausted-retries

Conversation

@lEWFkRAD

@lEWFkRAD lEWFkRAD commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

In run_conversation, api_call_count is incremented optimistically at the top of each loop iteration, before the API call actually succeeds. When a call fails after exhausting all retries — with no transport recovery and no fallback provider available — the terminal path counted that failed attempt as a successful API call, so the logged / returned / persisted count was one too high.

Related Issue

Fixes #38445

Type of Change

  • ðŸ�› Bug fix (non-breaking change that fixes an issue)

Changes Made

  • agent/conversation_loop.py — on the exhausted-retries terminal path, refund the optimistic bump before the count is logged, persisted, and returned (api_call_count -= 1; agent._api_call_count = api_call_count). Mirrors the refund the Ollama-context abort path already performs (conversation_loop.py:1102).
  • tests/run_agent/test_api_call_count_refund_on_exhausted_retries.py — new regression test.

How to Test

  1. pytest tests/run_agent/test_api_call_count_refund_on_exhausted_retries.py -q → 2 passing. Drives run_conversation with a mocked LLM that always raises a retryable transport error and asserts api_calls == 0 (plus a case verifying the count stays accurate after a prior successful call).
  2. Verified the tests fail on main and pass with the fix.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix
  • I've run pytest tests/ -q and all tests pass — ran the new module (2 passed) + py_compile; full suite not run in my Windows dev env
  • I've added tests for my changes
  • I've tested on my platform: Windows 11

Documentation & Housekeeping

  • Documentation — N/A (internal accounting fix, no user-facing surface)
  • cli-config.yaml.example — N/A
  • CONTRIBUTING.md / AGENTS.md — N/A
  • Considered cross-platform impact — pure Python, platform-agnostic
  • Tool descriptions/schemas — N/A

Notes

Intentionally scoped to the exhausted-retries terminal path the issue describes; other terminal/abort paths in run_conversation were left untouched (focused-PR rule). If any has a similar off-by-one it would be a separate issue.

run_conversation increments api_call_count optimistically at the top of
each loop iteration, before the API call is actually made. When a call
fails after exhausting all retries (and no transport recovery or fallback
is available), the terminal return path reported that failed attempt as a
successful API call, leaving the logged/returned/persisted count one too
high.

Refund the optimistic bump on the max-retries-exhausted path (decrement
api_call_count and update the agent._api_call_count mirror) before the
count is logged, persisted, and returned. This mirrors the refund the
Ollama-context abort path already performs.

Adds a regression test that drives run_conversation with a mocked LLM
that always raises a retryable transport error, asserting api_calls is
refunded to 0.

Fixes NousResearch#38445

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@lEWFkRAD lEWFkRAD closed this Jun 4, 2026
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Jun 4, 2026
@lEWFkRAD
lEWFkRAD deleted the fix/refund-api-call-count-on-exhausted-retries branch June 4, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: when all retries exhausted, the "api_call_count" is not refund.

3 participants