Skip to content

SWF(bug): count legacy completion prompt tokens once per request - #72

Merged
maplepaladin73 merged 1 commit into
mainfrom
devin/1795397216-legacy-completion-prompt-tokens
Nov 23, 2026
Merged

maplepaladin73 merged 1 commit into
mainfrom
devin/1795397216-legacy-completion-prompt-tokens

Conversation

@maplepaladin73

@maplepaladin73 maplepaladin73 commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

/v1/completions with n > 1 reported usage.prompt_tokens multiplied by n (n=2 with a 1-token prompt reported 2). Now each prompt is counted once.

Root cause: in run_blocking (crates/spark-server/src/api/completions_exec.rs), sum_prompt += prompt_tokens.len() sat inside the per-choice for n_i in 0..n loop, so the same prompt was added once per choice. The fix moves it to the outer per-prompt loop. Completion tokens are still summed across choices. For the same reason, cached prompt tokens now take the largest cached prefix per prompt instead of summing it per choice, which matches blocking chat and keeps cached_tokens <= prompt_tokens.

Test plan

New regression completion_usage_counts_prompt_once_for_multiple_choices calls the real handler with a scheduler fixture: n=2, 1-token prompt. It expects 2 choices, prompt 1, completion 2, total 3, cached 1.

Try it:

ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo test -p spark-server --bin spark completion_usage_counts_prompt_once_for_multiple_choices

Before the fix (main):

assertion `left == right` failed
  left: Number(2)
 right: 1
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 3266 filtered out; finished in 0.00s

After the fix:

test api::completions_exec::spec_policy_tests::completion_usage_counts_prompt_once_for_multiple_choices ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3266 filtered out; finished in 0.01s

Full CPU suite cargo test -p spark-server --bin spark: 3253 passed; 0 failed; 14 ignored.

  • cargo fmt --all -- --check
  • clippy (spark-server, ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000): clean
  • bash scripts/check-license-headers.sh: OK
  • typos: clean
  • Tested against a real model / hardware: not tested (CPU only, GPU hosts reserved)
  • Added a regression test

Notes for reviewers

Untested: live serving on GPU and streaming /v1/completions (this changes only the blocking path).

CLA


Software Factory opened this PR for Robert
Implemented in session Count legacy completion prompt tokens once per request
Review with Devin · Open in Devin Desktop


Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@maplepaladin73
maplepaladin73 merged commit 82e50c0 into main Nov 23, 2026
1 check 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.

1 participant