Skip to content

Add task_budget option for token budget management#747

Merged
qing-ant merged 1 commit intoanthropics:mainfrom
henrykeetay:henry/add-task-budget-support
Mar 26, 2026
Merged

Add task_budget option for token budget management#747
qing-ant merged 1 commit intoanthropics:mainfrom
henrykeetay:henry/add-task-budget-support

Conversation

@henrykeetay
Copy link
Copy Markdown
Contributor

Summary

  • Add TaskBudget TypedDict and task_budget field on ClaudeCodeOptions to let callers set an API-side token budget
  • Wire the option through SubprocessCLITransport as a --task-budget CLI flag
  • Export TaskBudget from the public package API

Test plan

  • Verify test_build_command_with_task_budget passes (python -m pytest tests/test_transport.py)
  • Verify test_build_command_without_task_budget passes to ensure no regression when the option is unset
  • Run full test suite (python -m pytest tests/)
  • Run type checking (python -m mypy src/)

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-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 26, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@d5ad876). Learn more about missing BASE report.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@henrykeetay
Copy link
Copy Markdown
Contributor Author

E2E Verification

Tested the task_budget option live against the SDK to confirm the flag flows through correctly.

Test: --task-budget CLI flag presence

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 qing-ant merged commit 2e60cec into anthropics:main Mar 26, 2026
7 checks 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.

3 participants