Multi-step model tasks need a way to track work and keep intermediate results outside the conversation. A deep agent built with the LangChain Deep Agents SDK combines a todo list, virtual files, and subagents around a tool-calling chat model.
The built-in general-purpose subagent can suggest material while the main agent manages the plan. StateBackend keeps the resulting files in the agent's in-memory state rather than on the computer's disk. Persistence across program exits needs separate storage.
A local Ollama model can perform this work without a provider API key. A running Ollama server, enough memory for the model and its context, and a dedicated Python 3.11+ environment are prerequisites. Generated plans and wording can vary between requests.
Steps to create a deep agent with LangChain:
- Activate the project virtual environment from your project directory.
$ source .venv/bin/activate
The environment needs Python 3.11 or newer for Deep Agents. A dedicated environment prevents dependency updates from changing another application.
Related: How to create a virtual environment for LangChain
Related: How to install Python 3 on Ubuntu or Debian - Pull the tool-calling qwen3:8b model into the running Ollama server.
$ ollama pull qwen3:8bThe model download is about 5.2 GB; inference also needs working memory for the model and context. A small text-only model without tool calling cannot run the SDK's planning and file tools.
Related: How to start the Ollama server
Related: How to pull an Ollama model - Install Deep Agents and the LangChain Ollama integration in the active environment.
Deep Agents uses LangChain 1.x and may update existing LangChain dependencies. A dedicated environment keeps those changes out of an older application. Tested with deepagents 0.7.37 and langchain-ollama 1.1.0 on Python 3.14.4.
Related: How to migrate a Python application to LangChain v1$ python -m pip install --upgrade deepagents langchain-ollama
- Create deep_agent.py in your text editor with the model connection below.
- deep_agent.py
import os from deepagents import create_deep_agent from deepagents.backends import StateBackend from langchain.agents.middleware import TodoListMiddleware from langchain_ollama import ChatOllama model = ChatOllama( model="qwen3:8b", base_url=os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434"), num_ctx=16384, num_thread=4, reasoning=False, temperature=0.7, top_p=0.8, top_k=20, num_predict=4096, )
num_ctx=16384 reserves room for the SDK instructions, tool definitions, and tool results. num_thread=4 limits CPU inference to four worker threads. reasoning=False sends Ollama’s hard non-thinking switch, which the built-in subagent inherits with this model. The sampling values follow Qwen3’s non-thinking recommendations. num_predict=4096 allows room for each response and its tool arguments while bounding generation; a response that reaches this limit may be incomplete. The local endpoint needs no API key; if Ollama is on another host, OLLAMA_BASE_URL must contain that server's base URL without /api. An unauthenticated Ollama listener should stay on a trusted local connection.
Related: How to connect LangChain to an Ollama chat model - Append the agent construction after the model definition in deep_agent.py.
agent = create_deep_agent( model=model, backend=StateBackend(), middleware=[TodoListMiddleware()], system_prompt=( "You prepare concise study plans. Use the planning, delegation, " "and virtual file tools when requested. Do not claim an action " "is complete until its tool has succeeded." ), )
TodoListMiddleware supplies the write_todos planning tool explicitly. create_deep_agent supplies virtual-file tools and the task tool for delegation. No custom subagent is needed to use general-purpose. StateBackend gives these file tools virtual storage without granting access to local directories or a shell.
- Append the study-plan request after the agent construction in deep_agent.py.
result = agent.invoke({ "messages": [{ "role": "user", "content": ( "Create a concise 30-minute Python study plan for someone " "who knows variables and loops. Call only one tool per " "response and wait for its result before the next call. " "First use write_todos to " "track your work. Call task for the general-purpose subagent " "with this self-contained description: Suggest two Python " "topics and one exercise per topic for a learner who knows " "variables and loops; answer in under 60 words without tools. " "Use write_file to save a plan under 100 words to " "/study_plan.md, then read_file to check it. Complete the " "todos with write_todos and answer in one sentence." ), }], })
The hard non-thinking setting applies to both the main and delegated requests. A subagent has its own conversation, so its description includes the learner’s Python level. One tool call per response keeps delegation, file writes, and file reads in dependency order. The model chooses the tool calls and their arguments. The delegated topic suggestions return to the main agent, which writes the plan using the SDK's virtual-file tool. No todo list or file is supplied in the initial state.
- Append the result inspection after the invocation in deep_agent.py.
print("Todos:") for todo in result["todos"]: print(f"- {todo['status']}: {todo['content']}") print("\nVirtual file /study_plan.md:") print(result["files"]["/study_plan.md"]["content"]) print("\nFinal answer:") print(result["messages"][-1].content)
todos contains the agent's plan, files holds virtual file data, and the last message contains the final response. A missing file raises KeyError instead of printing a claimed file from the model's prose.
- Save deep_agent.py in the project directory.
- Run deep_agent.py to inspect the todo state, virtual file contents, and final answer.
$ python deep_agent.pyExample output:
Todos: - completed: Complete the study plan creation and provide the final answer Virtual file /study_plan.md: # 30-Minute Python Study Plan ## Topics and Exercises 1. **Functions**: Learn to define and call functions, reuse code. - Exercise: Write a function that calculates the area of a rectangle. ##### snipped ##### Final answer: Here is your 30-minute Python study plan: ##### snipped #####
The todos should be completed, the virtual file should contain a 30-minute study plan, and the final answer should describe that plan. Wording, topic counts, and response length can differ from the request. A traceback, unfinished todos, an absent file, or a missing final answer indicates an incomplete request. The Python program remains available for another invocation; the printed virtual file belongs to this invocation's state.