CompiledSubAgent
CompiledSubAgent wraps an agent or LangGraph graph you already built, with a name and a description, so a deep agent can delegate to it through the task tool like any other subagent.
Last updated: 29 Sep, 2026 ยท Deep Agents 0.7
The video defines subagents as dictionaries. The docs add a second form for when you already have an agent: build it with LangChain's create_agent or as a LangGraph graph, then wrap it. The graph needs a messages key in its state, which create_agent has.
The CompiledSubAgent syntax
from deepagents import CompiledSubAgent
checker = CompiledSubAgent(name="budget-checker", description="...", runnable=my_graph)
agent = create_deep_agent(model=model, subagents=[checker])A budget checker built with create_agent
The checker is a plain LangChain agent with one tool, add_costs. The main agent must not do sums itself; it sends the amounts to the checker. Start trip.py with both tools, from Planning with write_todos:
from langchain.tools import tool
CATALOG = {
"paris": {
"flight": ["Return flight Delhi to Paris: 42,000 rupees"],
"hotel": ["Seine Budget Inn, Latin Quarter: 5,200 rupees a night",
"Hotel Lumiere, Montmartre: 7,500 rupees a night",
"Le Grand Opera Hotel: 16,000 rupees a night"],
"sight": ["Eiffel Tower summit: 3,100 rupees", "Louvre Museum: 2,000 rupees",
"Seine river cruise: 1,500 rupees", "Versailles day trip: 2,600 rupees",
"Montmartre walking tour: free"],
"food": ["Cafe breakfast and bistro dinner: 3,000 rupees a day"],
},
}
@tool
def search_travel(city: str, kind: str) -> str:
"""Search the travel catalog. kind is "flight", "hotel", "sight" or "food". Prices are in rupees."""
entries = CATALOG.get(city.lower(), {}).get(kind)
return "\n".join(entries) if entries else f"The catalog has no {kind} entries for {city}."
@tool
def add_costs(amounts: list[int]) -> int:
"""Add rupee amounts and return the exact total. Use it for every sum."""
return sum(amounts)from deepagents import create_deep_agent
from langchain.chat_models import init_chat_model
model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0, max_retries=6)from deepagents import CompiledSubAgent
from langchain.agents import create_agent
budget_graph = create_agent(
model,
tools=[add_costs],
system_prompt="Add the amounts you are given with add_costs. Reply with the total and whether it fits the budget.",
)
budget_checker = CompiledSubAgent(
name="budget-checker",
description="Adds up a list of trip costs and checks them against a budget.",
runnable=budget_graph,
)The main agent
agent = create_deep_agent(
model=model,
tools=[search_travel],
subagents=[budget_checker],
system_prompt="You plan trips. Look up prices with search_travel. Do not add numbers yourself: send the "
"separate amounts and the budget to budget-checker with the task tool. Answer in two short sentences.",
)Checking a trip against a 60,000 rupee budget
request = "Does a Delhi-Paris flight plus 3 nights at Hotel Lumiere fit a budget of 60,000 rupees?"
result = agent.invoke({"messages": [{"role": "user", "content": request}]})
for message in result["messages"]:
print(f"{message.type:<5}", message.text or [(c["name"], c["args"]) for c in message.tool_calls])Output
human Does a Delhi-Paris flight plus 3 nights at Hotel Lumiere fit a budget of 60,000 rupees?
ai [('search_travel', {'city': 'Delhi', 'kind': 'flight'})]
tool The catalog has no flight entries for Delhi.
ai [('search_travel', {'city': 'Paris', 'kind': 'hotel'})]
tool Seine Budget Inn, Latin Quarter: 5,200 rupees a night
Hotel Lumiere, Montmartre: 7,500 rupees a night
Le Grand Opera Hotel: 16,000 rupees a night
ai [('search_travel', {'city': 'Paris', 'kind': 'flight'})]
tool Return flight Delhi to Paris: 42,000 rupees
ai [('task', {'description': "Given the following costs: flight cost = 42000 rupees, hotel cost per night = 7500 rupees, number of nights = 3, and budget = 60000 rupees, determine whether the total fits within the budget. Return 'yes' if it fits, otherwise 'no'.", 'subagent_type': 'budget-checker'})]
tool no
ai The flight costs 42,000โฏrupees and the hotel for three nights costs 22,500โฏrupees, totaling 64,500โฏrupees. This exceeds the 60,000โฏrupee budget, so it does not fit.What the run shows
- The main agent looked up the prices with
search_travel. Its first try used Delhi as the city and found nothing, so it searched Paris for the hotels and the flight. - It called task with subagent_type budget-checker and put the separate amounts, the nights and the budget in the job description.
- The checker answered in one word, "no": the job description asked for yes or no.
- The reply is no, with the total, 64,500 rupees. The main agent worked that figure out for its explanation even though its prompt said not to add numbers itself; a prompt rule is advice, and the checker's verdict is the part it could not change.
Dictionary subagent vs CompiledSubAgent
| Dictionary (SubAgent) | CompiledSubAgent | |
|---|---|---|
| You give | Name, description, prompt, tools | Name, description, a built graph |
| Deep Agents builds it | Yes, with file tools and summarization | No, it runs your graph as it is |
| Inherits the main agent's interrupt_on | Yes | No |
Where a compiled subagent fits
- You already have a working LangChain agent or LangGraph workflow.
- A subagent that must stay small: no file tools, only its own tool.
- A graph with custom steps that a prompt and tools cannot express.
Watch out. A compiled subagent does not inherit the main agent's human-in-the-loop settings. If it has a risky tool, configure approval inside the graph you wrap.
Related
- Previous: Subagent structured output
- Next: Human-in-the-loop
- Reference: Subagents: CompiledSubAgent
Try it yourself
- Raise the budget to 70,000 rupees and read the checker's answer.
- Swap Hotel Lumiere for Le Grand Opera Hotel in the request.
- Print
budget_graph.get_graph().nodesto see the checker's own nodes.
This is what real progress feels like.