7.5

A deep agent with a subagent and files

A deep agent on a local model plans, writes, and delegates a review in seven model calls, and its first call sends 3,651 prompt tokens for a short task.

You want an agent that drafts a document, asks a second agent to review it, and applies the fixes. Deep Agents gives you the file tools, the task tool, and the subagents in one call. You still need to know what each call costs, and which part of a bad result came from the model.

When you finish this section, you can build a deep agent with one subagent on a local model. You can also record the tokens of each model call and read a run call by call.

What the demo builds

The code is our own. It uses deepagents 0.7.38 with ChatOllama and Qwen3 8B, at temperature 0, seed 42, and with reasoning off.

The demo keeps the default backend, which holds the files in the agent state, so you can read every call. On the task suite of section 1.2, that setup lost to a plain loop on Qwen3 4B and 8B, on file and retrieval tasks. When the work is on real files, pass FilesystemBackend, as section 7.4 shows.

The demo applies these sections:

The agent has one subagent, editor. A subagent is a dict with a name, a description that the main agent reads, and a system prompt of its own:

pythonthe deep agent and its editorFull log
    agent = create_deep_agent(
        model=model,
        system_prompt="You write clear technical documents. Keep the files you write in /notes/.",
        subagents=[{**EDITOR, "model": model}],
        middleware=[TodoListMiddleware()],
        checkpointer=InMemorySaver(),
    )

The task asks for release notes of three changes, a review by the editor, the fixes, and the final text. The system prompt of the editor asks for sentences over 25 words and sentences in the passive voice. The editor returns fixes and edits nothing.

A callback records each model call. It reads usage_metadata from the reply and lc_agent_name from the metadata of the call. Deep Agents sets that name to editor for the subagent, so the callback can tell the two agents apart.

The recorded run

The run made seven model calls in 62.0 s. The first call took 22.6 s, which includes the load of the model into Ollama:

outputthe seven model callsFull log
  call  1  main    in  3651  out  120   22.6 s  -> write_todos
  call  2  main    in  3872  out  124    6.7 s  -> write_file
  call  3  main    in  4016  out   58    3.2 s  -> task
  call  4  editor  in  1977  out   27    8.4 s  -> read_file
  call  5  editor  in  2113  out  143    7.4 s  -> answer
  call  6  main    in  4225  out  104    6.3 s  -> write_todos
  call  7  main    in  4446  out  111    7.4 s  -> answer

The main agent wrote a todo list, wrote /notes/release-2.0.md, and called task. The editor read the file and answered. The main agent then marked its todos and replied with the text of the file. It never called edit_file, because the editor returned no fixes:

outputwhat the editor returnedFull log
  result    task: The file `/notes/release-2.0.md` has been read successfully. Here is the content: ``` ## Version 2.0 Release Notes The sync command now resumes an interrupted upload from the last finished part, ensuring seamless continuity for large file transfers. … ``` No sentences in this file exceed 25 words, and there are no sentences in passive voice. Therefore, no fixes or improvements are needed.
The copy of the file inside the reply is cut at the mark.

The file that the run wrote holds two sentences in the passive voice:

outputtwo sentences of the final fileFull log
The config file has been moved from ~/.tool.toml to the XDG config folder. The old path still works for one release, ensuring backward compatibility during the transition.

The --verbose flag has been removed. Use --log-level debug in its place for detailed logging and debugging information.

What the run shows

We measured one run on the Mac, Apple M1 Max, 10 cores, 64 GiB, with Ollama 0.31.1. The main agent made 5 model calls with 20,210 prompt tokens. The editor made 2 calls with 4,090 prompt tokens. Together they sent 24,300 prompt tokens and got 687 output tokens, for a file of 458 characters. Figure 7.10 shows each call. Raw runs

Fig. 7.10prompt tokens of each model call of the deep agentchart
Prompt tokens of each model call of the deep agent A dot chart of the prompt tokens of the 7 model calls of one deep agent run on the Mac with Qwen3 8B. The main agent made 5 calls of 3651, 3872, 4016, 4225, 4446 tokens, 20210 in all, with 9 tools bound. The editor subagent made 2 calls of 1977 and 2113 tokens, 4090 in all, with 7 tools bound. 0 1k 2k 3k 4k 5k 1 2 3 4 5 6 7 model call, in the order of the run prompt tokens of the call 3651 write_todos 3872 write_file 4016 task 1977 read_file 2113 answer 4225 write_todos 4446 answer main agent: 5 calls, 20210 tokens editor subagent: 2 calls, 4090 tokens The label on the axis is the tool that the call asked for. Calls 4 and 5 ran inside the task tool, with 7 tools in place of 9.
Every call of the main agent sent more than 3,600 prompt tokens, and the editor calls sent about half that, with two tools fewer. Each dot is one model call of one run on the Mac with Qwen3 8B. Blue dots are the main agent and indigo dots the editor. The label on the axis is the tool the call asked for.

The first call of the main agent sent 3,651 tokens for a task of about 100 words. Most of that is the harness: the system prompt of Deep Agents and the schemas of 9 tools. Section 7.4 measured the extra cost of the harness at 2,075 to 2,251 tokens per call on Qwen3 8B. Each later call adds the messages of the run on top.

The editor had 7 tools: the file tools without task and write_todos. Its messages held its own system prompt and the task description from the main agent. So its calls were about half the size. A subagent is a way to keep a long side task out of the context of the main agent.

The review failure is a model result. The editor missed two sentences in the passive voice, and the main agent trusted the review. LangChain ran each step that the model asked for: the file write, the task call, the read, and the todo updates. A stronger model, or a check in code for the passive voice, could change the outcome. The first is a choice of model, and the second is a tool you write.

LangChain (Python): langchain, langchain-core, langgraph, deepagents, langsmith 1.5.0 · c2f4036 · 2026-11-11 · Edition 2026.11