Skip to content

fix: Accumulate retry costs instead of overwriting in _LAST_CALLBACK_DATA#519

Merged
gltanaka merged 10 commits intopromptdriven:mainfrom
Serhan-Asad:fix/issue-509
Feb 16, 2026
Merged

fix: Accumulate retry costs instead of overwriting in _LAST_CALLBACK_DATA#519
gltanaka merged 10 commits intopromptdriven:mainfrom
Serhan-Asad:fix/issue-509

Conversation

@Serhan-Asad
Copy link
Contributor

Summary

  • Fixes cost under-reporting when llm_invoke retries due to None content, malformed JSON, or invalid Python
  • _LAST_CALLBACK_DATA["cost"] was being overwritten on each retry call, silently losing the original call's cost
  • Fix saves and accumulates cost/tokens before each retry across all 3 retry paths (~lines 2455, 2503, 2749)

Verified with real API calls

Branch Reported Cost LLM Calls
main (buggy) $0.000340 2 (only retry cost reported)
fix branch $0.000528 2 (both costs accumulated)

Test plan

  • Unit tests: tests/test_llm_invoke_retry_cost.py (4 tests)
  • E2E tests: tests/test_e2e_issue_509_retry_cost.py (2 tests)
  • All 6 tests fail on main, pass on fix branch
  • Full test suite: 773 passed, no regressions
  • Manual test with real OpenAI API confirming cost accumulation

🤖 Generated with Claude Code

Serhan-Asad and others added 3 commits February 12, 2026 14:57
Unit and E2E tests that verify _LAST_CALLBACK_DATA["cost"] accumulates
across retries instead of being overwritten. Tests currently fail,
confirming the bug.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…urvive importlib.reload

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gltanaka gltanaka requested a review from Copilot February 13, 2026 23:20
@gltanaka
Copy link
Contributor

you need to fix the unit tests

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes under-reported LLM spend when llm_invoke() triggers cache-bypass retries by accumulating _LAST_CALLBACK_DATA cost/token totals across retry paths, and adds regression tests for issue #509.

Changes:

  • Accumulate cost + token totals across retries (None content, malformed JSON, invalid Python) instead of overwriting _LAST_CALLBACK_DATA.
  • Expand malformed-JSON detection to include excessive actual trailing newlines.
  • Add unit + “E2E-style” tests covering retry cost accumulation behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

File Description
pdd/llm_invoke.py Saves pre-retry callback totals and adds them back after retry completion; expands malformed-JSON heuristic.
tests/test_llm_invoke_retry_cost.py Adds unit tests asserting cost accumulation across retry scenarios.
tests/test_e2e_issue_509_retry_cost.py Adds higher-level tests simulating retry behavior and verifying accumulated cost.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Serhan-Asad and others added 7 commits February 13, 2026 18:30
The CSV had empty run_test_command for JavaScript, TypeScript, and
TypeScriptReact, causing 5 CI test failures in test_agentic_langtest,
test_get_language, and test_get_test_command.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…y condition

- Add token accumulation assertions to test that claims to verify tokens
- Remove unused imports (csv, CliRunner) from e2e test
- Fix misleading docstring about CliRunner usage
- Trim 3-call setup to 2-call to match actual retry behavior
- Simplify redundant endswith('}') check in _is_malformed_json_response

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… retry failure

All 3 retry paths (None content, malformed JSON, invalid Python) set
litellm.cache = None but only restored it on success. If the retry call
raised an exception, cache stayed permanently disabled for the process.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Resolve conflict: keep upstream's language_format.csv (was accidentally
deleted on this branch).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Serhan-Asad Serhan-Asad marked this pull request as ready for review February 15, 2026 22:57
@gltanaka gltanaka merged commit 1cb477c into promptdriven:main Feb 16, 2026
2 checks passed
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.

2 participants