Published on April 24, 2026
With LangChain, you can connect multiple agents using the Deep Agents feature. A basic example of this is shown below where a division agent and a weather agent are used as subagents for the main agent (deep agent).
The divider and weather subagents are defined below. Each subagent can be invoked individually from the command line which is useful for testing and evaluation. The agents use the OpenAI GPT model so an OPENAI_API_KEY is needed to run the code.
# agent_divider.py
from langchain.agents import create_agent
def divide_by_two(x: float | int) -> float:
"""Divide a number by 2."""
result = float(x) / 2.0
return result
agent = create_agent(
name="division-agent",
model="openai:gpt-5.4-mini",
tools=[divide_by_two],
system_prompt="You are a math agent that divides a number by 2",
)
if __name__ == "__main__":
result = agent.invoke(
{"messages": [{"role": "user", "content": "What is half of 25"}]}
)
text = result["messages"][-1].text
print(text)
# agent_weather.py
from langchain.agents import create_agent
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}! There is no wind, temperature is 72 F, slight change of rain."
agent = create_agent(
name="weather-agent",
model="openai:gpt-5.4-mini",
tools=[get_weather],
system_prompt="You are a helpful weather forecast agent",
)
if __name__ == "__main__":
result = agent.invoke(
{"messages": [{"role": "user", "content": "What is the weather in paris"}]}
)
text = result["messages"][-1].text
print(text)
The main agent, which is a deep agent, is shown next. Notice how the divider and weather agents are used by the deep agent as subagents. In this example, the deep agent will invoke the subagents to get the current weather conditions and divide the temperature by two.
# agent.py
from deepagents import create_deep_agent, CompiledSubAgent
from agent_weather import agent as agent_weather
from agent_divider import agent as agent_divider
def main():
"""Run the main agent."""
weather_subagent = CompiledSubAgent(
name="weather-subagent",
description="Specialized agent for weather forecasts",
runnable=agent_weather,
)
divider_subagent = CompiledSubAgent(
name="divider-subagent",
description="Specialized agent for math division",
runnable=agent_divider,
)
agent = create_deep_agent(
model="openai:gpt-5.4-mini",
system_prompt="You are a helpful assistant that uses subagents for specialized tasks",
subagents=[weather_subagent, divider_subagent],
)
result = agent.invoke(
{
"messages": [
{
"role": "user",
"content": "Get the weather in paris then divide the temperature",
}
]
}
)
print("\n--- Result ---\n", result)
text = result["messages"][-1].text
print("\n--- Text ---\n", text)
if __name__ == "__main__":
main()
The pyproject.toml file for defining the dependences and running the code in a Python environment with uv is given here.
[project]
name = "multi-agents"
version = "0.1.0"
requires-python = ">=3.14"
dependencies = [
"deepagents>=0.5.3",
"langchain-openai>=1.1.14",
]
See the LangChain docs for more information about Deep Agents.
Gavin Wiggins © 2026
Made on a Mac with Genja. Hosted on GitHub Pages.