techniques: ctx.add_cost is per-step (ContextVar) — no more lost or double-counted dollars under gather - #38
Open
ChrisW09 wants to merge 1 commit into
Open
techniques: ctx.add_cost is per-step (ContextVar) — no more lost or double-counted dollars under gather#38ChrisW09 wants to merge 1 commit into
ChrisW09 wants to merge 1 commit into
Conversation
_manual_cost was plain instance state shared by every step: a compose that gathers two ctx.run calls had stage B's reset wipe stage A's accrued dollars, and both steps read the same residual — costs vanished or were double-counted (verified: $0.11 of add_cost reported as $0.02). A compose-body add_cost was likewise wiped by the next ctx.run's reset, or dropped entirely after the last one. The manual-cost sink is now a ContextVar set around each technique call — the same isolation pattern the LLM usage sink already uses, so gathered steps (tasks copy their context) each accrue their own costs. add_cost outside any step charges the run's total_cost directly instead of being dropped. Fixes #12 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #12.
Context._manual_costwas shared instance state, reset at the start of everyctx.runand read at the end — so overlapping steps corrupted each other. Verified failures:asyncio.gather(ctx.run("ret_a"), ctx.run("ret_b"))where A adds $0.10 and B adds $0.01 reported a total of $0.02 (A's dollars vanished, B's double-attributed);ctx.add_costin the compose body was wiped by the next step's reset or dropped after the last one. The module docstring explicitly invites this control flow ("branches, cascades, loops — the topology is entirely yours").Fix: the manual-cost sink is now a
ContextVarset/reset around each technique call — the exact isolation patternllm._usage_sinkalready uses (asyncio tasks copy their context, so gathered steps get independent sinks; nestedctx.runrestores the outer sink on exit).add_costcalled outside any step (compose body) charges the run'stotal_costdirectly, so dollars are never silently dropped.Regression tests: concurrent-stages attribution (exact per-stage and total assertions) and compose-body accrual. The existing manual-cost test (
test_per_technique_and_manual_cost_flow_to_stage_report) passes unchanged.Test: full cafe-core suite — 112 passed (1 pre-existing failure = #4, fix in PR #34).
🤖 Generated with Claude Code