Add task_budget option for token budget management#747
Merged
qing-ant merged 1 commit intoanthropics:mainfrom Mar 26, 2026
Merged
Add task_budget option for token budget management#747qing-ant merged 1 commit intoanthropics:mainfrom
qing-ant merged 1 commit intoanthropics:mainfrom
Conversation
Add TaskBudget type and task_budget option to ClaudeCodeOptions, allowing callers to set an API-side token budget. When configured, the CLI receives a --task-budget flag so the model can pace tool use and wrap up before hitting the limit. - Add TaskBudget TypedDict with a `total` field - Wire task_budget through SubprocessCLITransport command building - Export TaskBudget from the public API - Add unit tests for command construction with task_budget
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #747 +/- ##
=======================================
Coverage ? 83.24%
=======================================
Files ? 14
Lines ? 2155
Branches ? 0
=======================================
Hits ? 1794
Misses ? 361
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
E2E VerificationTested the Test:
|
| Scenario | --task-budget in CLI args |
Query result |
|---|---|---|
task_budget=None (default) |
Not present | PASS |
task_budget={"total": 50000} |
--task-budget 50000 |
PASS |
task_budget={"total": 200000} |
--task-budget 200000 |
PASS |
Live query output
============================================================
WITHOUT task_budget
============================================================
CLI args: --task-budget NOT present
Assistant: Four
Result: cost=$0.101570, input_tokens=19319, output_tokens=199
============================================================
WITH task_budget = 50000
============================================================
CLI args include: --task-budget 50000
Assistant: Four
Result: cost=$0.032418, input_tokens=5020, output_tokens=6
============================================================
WITH task_budget = 200000
============================================================
CLI args include: --task-budget 200000
Assistant: Four
Result: cost=$0.036338, input_tokens=5019, output_tokens=163
All three cases complete successfully. The CLI accepts the --task-budget flag without error, and the flag is correctly omitted when task_budget is not set.
qing-ant
approved these changes
Mar 26, 2026
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.
Summary
TaskBudgetTypedDict andtask_budgetfield onClaudeCodeOptionsto let callers set an API-side token budgetSubprocessCLITransportas a--task-budgetCLI flagTaskBudgetfrom the public package APITest plan
test_build_command_with_task_budgetpasses (python -m pytest tests/test_transport.py)test_build_command_without_task_budgetpasses to ensure no regression when the option is unsetpython -m pytest tests/)python -m mypy src/)