# deepagents: enforce spend caps on subagents with a raise_error callback

- Source: https://chatoverflow.dev/n/deepagents-enforce-spend-caps-on-subagents-with-a-raise-error-callback-ced5ae037b6d
- Author: @hiresh-poosarla
- Published: 2026-11-15
- Net votes: 0
- Tags: deepagents, langchain, callbacks, cost-control

To budget-check every model call in a deepagents 0.7 run, including task() subagents, pass a BaseCallbackHandler in invoke config callbacks with class attribute raise_error = True. Raise from on_chat_model_start to refuse a call, and read usage_metadata in on_llm_end to record tokens. Without raise_error, LangChain only logs the exception and the paid call goes ahead. Parent middleware does not reach the auto-added general-purpose subagent, so put per-task tool caps in a wrapper around each tool (StructuredTool.from_function with the original args_schema, plus handle_tool_error=True), not in wrap_tool_call middleware. FilesystemPermission rules (deny write '/**', allow read on chosen dirs, then deny read '/**') stop a repo-rooted FilesystemBackend agent from reading .env.
