Meta: quota exhaustion reports need per-request usage in wire logs to be actionable

Open
#5,368 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
15/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript
Domain
ai, tooling

Research direction

Start in loopService.ts around dynamicTools (about line 638 for normal turns, 861 for operation requests) and operationTools() (about line 658), and compare how the compaction request resolves its tools block with a normal turn. The open question is which tool-resolution approach to use, and the issue is still awaiting maintainer approval. Done means the compaction request keeps the same tools block as a normal turn, so the cached prefix is reused, and the wire log records the per-request fields the issue describes.

Written by the indexing model from the issue text.

Description

Several reports describe quota being consumed faster than expected. Grouping them here because they need the same kind of evidence to become actionable, and because at least one of them has a measured cause we can fix.

Primary: #679, #3948, #3781, #3926. All are still open.

One measured case

#4935 records a compaction with 941,639 input tokens and only 26,368 cached (2.8%). The reporter's controlled follow-up found the cause: with a tool allowlist active, a normal request sends about 40 tool definitions but the compaction request sends 30, and cache hits drop from 98.1% to 2.6%. Tool definitions sit at the front of a request, so a change there invalidates the cached prefix for the system prompt and everything after it — extra billable input on every compaction.

The other reports lack the data to say what happened. #679 reports 26 interactions in 90 seconds with two log lines and no reproduction steps. #3948 is a screenshot plus "does not follow instructions, wastes tokens". #3781 names 0.7.5 as a regression and asks whether context is being re-sent; #3926 reports a stuck turn followed by a 403. None of these can currently show whether the quota went to repeated requests, one uncached request, a changed tools block, or a request that never completed — and a maintainer cannot tell from the metadata either.

Proposed direction

We are preparing two changes and will share the full patch in a PR once this issue is approved:

  1. Keep the tools block stable across compaction. Normal turns resolve tools dynamically (dynamicTools: true, loopService.ts:638), but operation requests default to dynamicTools: false (loopService.ts:861) and operationTools() (loopService.ts:658) filters a startup snapshot of tools — it can only remove, never add. That is why the compaction request ends up with a different tools block and breaks the cached prefix. The direction is to make the compaction request use the same tool resolution as a normal turn. We are still weighing a one-line dynamicTools: true against having operation inherit the previous turn's exact tools snapshot; the trade-off is whether deferred-tool schemas leak into the operation, and will be resolved in the PR.
  2. Make future reports self-evident. Add per-request fields to the wire log — turn and step ids, a hash of the tools block, cache hit count, and the step-end reason — so a report can show whether quota went to repeated requests, one uncached request, a changed tools block, or a request that never completed.

Full diffs, edge cases (deferred tools, allowlist changes, profile switches), and tests will come with the PR. We are deliberately not treating all four reports as one root cause: #4935 is proven, #3926 is plausibly the stuck-turn problem tracked separately, #679 and #3948 need logs before any conclusion, and #3781 needs a version-by-version comparison. A session-level spending cap is a product decision and is out of scope.

Request

Would a maintainer /approve this issue so we can open an external bug-fix PR linking it? We would scope the PR to the measured #4935 cache fix and the logging change, and leave the symptom-only reports for follow-up. Happy to wait for triage or narrow further if needed.

This is based on code audit and the reports linked above; I do not have a runnable checkout, so the test suite and a live A/B measurement will be part of the PR.

Dominant language
TypeScript
Stars
8.3k
Forks
1.4k
Avg merge
12h 6m
Merged PRs (30d)
596

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MoonshotAI/kimi-code

All issues in MoonshotAI/kimi-code

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.