Skip to content

loom code describe stops at --limit and --max-usd and prints what it spent - #1807

Merged
zlareb1 merged 3 commits into
mainfrom
fix/describe-limit-spend
Nov 30, 2026
Merged

zlareb1 merged 3 commits into
mainfrom
fix/describe-limit-spend

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Related: #1679

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

loom code describe --limit N now starts at most N description calls, and the command ends with one line that counts the chunks described, already described and remaining, the tokens used and their price. A new --max-usd flag stops the run before a call that would take its spend past a dollar cap. Both flags refuse a value below 0.

Why

An operator who built the description layer for a set of repositories could not cap the run and saw no figure for what it cost. Three things were checked in the source:

  1. --limit bounded nothing that the run had already queued. The stop flag was read once, at the top of each file and chunk coroutine, before the coroutine waited on its semaphore. asyncio.gather starts every coroutine before the first description lands, so all of them were past the check when the flag was set. With 3 files of 10 pending chunks, --limit 5 --concurrency 8 made 30 provider calls.
  2. The command printed describe complete: N chunks and nothing about usage. The provider clients report tokens and dollars to a cost accumulator, and the command never bound one, so the figures were dropped.
  3. An interrupted run does not describe a chunk twice. Each description is written as soon as it is embedded and a rerun skips rows whose context_source is llm. This was already true; the change adds a test for it.

The fix admits a description after it holds its concurrency slot. The count of calls started moves in the same step as the check, with no await between them, so concurrent chunks cannot pass the bound together. The limit counts calls started, not descriptions stored: a call whose reply is dropped still spent tokens.

Each description runs under its own accumulator (cost_accumulator.isolated_op), and the run sums them. The last line reads, for example:

describe complete: 5 described, 0 current, 25 remaining (stopped at --limit 5); 5000 input + 250 output tokens, 1000 embedding tokens, $0.0060

Dollars come from the server's price table and cover the description calls and the re-embedding. When a model that reported usage has no price, the line names it and gives tokens only. The Gemini and OpenAI-compatible embedders report no usage, so with them the line prints 0 embedding tokens and the dollars are a lower bound; the operator page says so.

--max-usd D reserves the cost of the dearest description so far for each call in flight and for the next one, and stops when the spend plus those reservations would pass D. That reservation is 0 until a description reports a cost, so descriptions run one at a time until one does: a first call that fails or reports no usage leaves the run serial instead of admitting every waiting call against a reservation of 0. A stricter bound from list prices was considered and left out: for a model with hidden reasoning the worst case of one call is the reply cap plus the full reasoning reserve, many times what a description costs, so the run would stop long before the cap. The cost of that choice is stated in the docs: a call that costs more than every description before it can take the run past D by that difference, once for each such call in flight.

The flag exits 2 before any call when the price table lacks the description model, or lacks the embedding model and its provider reports usage (OpenAI, Bedrock). For an embedding provider that reports no usage the cap covers the description calls only. A run still stops once any call reports usage on a model without a price.

Scope and risk

  • Touches loom code describe only: server/cli/code_maintenance.py and its parser in server/cli/code.py. The ingest pass and the description prompt are unchanged.
  • A stopped run still reads every selected file to count what is left. Every run read every selected file before this change too, so the read load is the same; only model calls stop.
  • The completion line changed its wording. Nothing in the repository parses it.
  • The cli.code.describe span gains current, remaining, input_tokens, output_tokens and cost_usd counters.
  • Left alone: the command still writes no row to the server's spend ledger, and an interrupted run prints no summary (the next run's summary counts what is left).

Compatibility, rollout and rollback

  • Wire: none. CLI: --max-usd added (default 0, no cap); --limit keeps its name and default and now holds. Both refuse a value below 0, which --limit accepted before (a negative limit bounded nothing).
  • Operator steps: none.
  • Rollback: reverting leaves nothing behind; descriptions already written stay valid.

Tests

Run locally on Python 3.12 with LOOM_ENV_FILE= set empty:

  • tests/unit/test_code_describe_bounds.py and tests/unit/test_code_description_provider_guard.py, loaded and run with unittest: 20 tests pass. The 12 new tests use a provider double that counts its calls and reports usage the way a provider client does: the limit holds across file boundaries with 8 calls in flight, a dropped reply still counts toward the limit, the summary equals the provider's counted tokens and their table price, --max-usd stops before the call that would pass it, it stays serial after a first call that fails, its overshoot with 3 dearer calls in flight is within 3 differences, it refuses an unpriced description model and an unpriced embedding model of a usage-reporting provider, it stops on usage reported by an unpriced model of another provider, an unpriced model prints tokens only, a rerun describes nothing, a rerun after a limit describes only the rest, and the parser refuses --limit -1 and --max-usd -1.
  • test_max_usd_stays_serial_until_a_description_has_a_cost failed before the fix it covers with (9, 8) != (3, 1) (calls, most in flight).
  • The body of test_loom_code_describe_leaves_a_marked_chunk_alone (tests/unit/test_code_embed_exclude.py), called from a snippet: passes.
  • The two bodies of tests/unit/test_cli_surface_snapshot.py, called from a snippet with LOOM_UPDATE_CLI_SNAPSHOT=1, regenerated the CLI snapshots; a second call without it passes.
  • ruff format --check, ruff check, the function-length gate, the secret gate and every generator --check that make lint runs pass on the head.
  • The mypy gate reports 5 errors, all in server/llm/vertex_auth.py, from an untyped google.auth in the local environment; none in a changed file.
  • The four source-tree checks of tests/unit/test_public_copy.py, called from a snippet: pass.

Not run locally: the full unit suite, the integration suite, the Biome lint. Nothing was run against a real ClickHouse or a real provider.

Measurement, with an in-memory index of 3 files of 10 pending chunks, concurrency 8, and a provider double that reports 1000 input and 50 output tokens per call. One description and its re-embedding cost $0.0012 at the doubles' prices. "Before" is main for the first row and the first pushed head of this pull request for the rows about the cap:

Run Before After
--limit 5 30 provider calls, 30 described, describe complete: 30 chunks 5 calls, 5 described, 25 remaining, 5000 input + 250 output tokens, $0.0060
--max-usd at 2.5 descriptions' price 2 calls, $0.0024 2 calls, 28 remaining, $0.0024
the same cap, first call fails 9 calls, 8 in flight together, 8 paid, $0.0096 against a cap of $0.0030 3 calls, never 2 in flight, 2 paid, $0.0024
--max-usd at 4.5 descriptions' price, later calls cost $0.0032 4 calls, 3 in flight, $0.0108 4 calls, 3 in flight, $0.0108: $0.0054 past the cap, within 3 × $0.0020
--max-usd with an unlisted embedding model that reports usage 1 paid call, then stopped exit 2, 0 calls
--limit -1 accepted: 30 calls on main, 0 calls and stopped at --limit -1 on the first pushed head exit 2, argument --limit: must be 0 or more
rerun after a full run 0 calls 0 calls, 0 described, 30 current, 0 remaining

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. Checked by running the before and after measurement above, the focused tests and the gates listed under Tests.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: docs/reference/cli.md and the CLI snapshots.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code


Note

Low Risk
Scope is the optional loom code describe operator path only; no API or ingest changes, though operators should expect a new completion line format.

Overview
loom code describe now honors --limit and adds --max-usd, and finishes with a one-line spend summary instead of only a chunk count.

The concurrency bug is fixed: description work is admitted after taking the semaphore, with limit and dollar checks and the “started” counter done in one step (no await between check and increment), so parallel chunk tasks cannot blow past --limit the way the old early stop flag allowed.

Each description runs under cost_accumulator.isolated_op(); a new _DescribeRun tracks described vs already-described vs remaining chunks, LLM and embedding tokens, and priced USD (or token-only when a model has no table entry). --max-usd refuses unpriced models up front (exit 2), reserves spend from the costliest call seen so far for in-flight work, and can serialize the first capped calls until a real cost is known.

CLI parsing gains _count / _dollars validators; docs and CLI snapshots document the new flag and summary format. Unit tests cover limits, caps, unpriced models, and resume behavior.

Reviewed by Cursor Bugbot for commit a232dd9. Bugbot is set up for automated code reviews on this repo. Configure here.

zlareb1 and others added 2 commits November 30, 2026 18:57
…at it spent

`--limit N` bounded nothing a run had already queued: the stop flag was read
once at the top of each file and chunk coroutine, before it waited on its
semaphore, and every coroutine had started before the first description
landed. Over 3 files of 10 pending chunks, `--limit 5 --concurrency 8` made 30
provider calls. The command also printed no usage: provider clients report to
a cost accumulator that the command never bound.

A description is now admitted after it holds its concurrency slot, and the
count of calls started moves with the check, so at most N calls start. Each
description runs under its own accumulator and the run sums them into its
last line: chunks described, already described and remaining, input, output
and embedding tokens, and dollars from the price table, or the unpriced model's
name. `--max-usd` reserves the dearest description so far for each call in
flight and for the next one, runs the first description alone to learn that
cost, and refuses a description model without a price. A stopped run still
reads every selected file, as every run did before, to count what is left.

The CLI help snapshot is regenerated under Python 3.12: `--limit` has new help
text and `--max-usd` is added.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Nov 30, 2026 1:42pm UTC
loom-telemetry Ready Ready Preview Nov 30, 2026 1:42pm UTC

Request Review

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2891b08. Configure here.

Comment thread server/cli/code_maintenance.py Resolved
…ne reports a cost

The cap reserves the cost of the dearest description so far for each call in
flight. That cost is 0 until a description reports usage, and the serial wait
ended at the first settle whether or not it reported any: after a first call
that failed, every waiting call was admitted against a reservation of 0. With
concurrency 8 and a cap of 2.5 descriptions' price, 9 calls were made and 8
paid for. The wait now holds while a call is in flight and no description has
a cost, so the same run makes 3 calls and pays for 2.

Also:
- the operator page states the overshoot as one difference for each dearer
  call in flight, and that an embedding provider reporting no usage makes the
  dollars a lower bound;
- an unlisted embedding model of a provider that reports usage is refused
  before the first call, not after one paid description;
- `--limit` and `--max-usd` refuse values below 0;
- `settle` takes the accumulator by its type.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1 zlareb1 changed the title loom code describe --limit stops at its limit and prints what it spent loom code describe stops at --limit and --max-usd and prints what it spent Nov 30, 2026
@zlareb1
zlareb1 merged commit 78f9fc8 into main Nov 30, 2026
78 of 80 checks passed
@zlareb1
zlareb1 deleted the fix/describe-limit-spend branch November 30, 2026 15:21
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