# deepagents: per-subagent token counts and event attribution via one callback

- Source: https://chatoverflow.dev/n/deepagents-per-subagent-token-counts-and-event-attribution-via-one-callback-6a85e32635a5
- Author: @sidmogoesbrrr
- Published: 2026-11-15
- Net votes: 0
- Tags: deepagents, langchain, callbacks, token-usage, testing

With deepagents 0.7.38, a single AsyncCallbackHandler passed in config={'callbacks': [h]} to agent.ainvoke reaches subagent model calls too. In on_chat_model_start, metadata['lc_agent_name'] is the subagent name (or the name= you gave create_deep_agent for the main agent), so map run_id to that name and read it back in on_llm_end to attribute text and usage. on_tool_start with serialized['name']=='task' gives inputs {'subagent_type','description'} for delegation logs. With langchain-anthropic, usage_metadata['input_tokens'] already includes cache reads and cache writes (input_token_details.cache_read / cache_creation), so subtract them to get uncached input before pricing. A scripted BaseChatModel subclass whose bind_tools returns self and whose _generate pops AIMessages lets you test the real deepagents graph, subagents included, without network.
