Ollama vs LangChain
Duration: 7 min Prerequisites: chapter 03
Key idea
Section titled “Key idea”LangChain is an excellent framework for orchestrating several commercial LLMs in complex projects. To learn how a local agent works, it adds an abstraction layer that hides exactly what we want to understand. So we skip it.
What LangChain does (and why it’s useful elsewhere)
Section titled “What LangChain does (and why it’s useful elsewhere)”LangChain solves real problems:
- switch provider (OpenAI → Anthropic → Mistral) without rewriting your code;
- chain several LLMs (one to summarise, one to draft, one to review);
- integrate RAG (document search) with vector stores;
- long-term memory, persistent conversations, callbacks, packaged multi-tool agents;
- a huge community with hundreds of ready-made integrations.
If you’re building a production product that has to juggle several providers and needs serious RAG, LangChain is probably a good choice.
Why not in this course
Section titled “Why not in this course”To explain an agent to a beginner, we want to see every message sent to the model, see the format of the tool_calls, see the returned result. The pedagogical value of the repo comes precisely from the fact that nothing is hidden.
Compare. With LangChain, it would look roughly like:
from langchain.agents import initialize_agent, AgentTypefrom langchain_community.llms import Ollamafrom langchain.tools import Tool
agent = initialize_agent( tools=[Tool.from_function(write_file, name="write_file", description="..."), ...], llm=Ollama(model="llama3.1:8b"), agent=AgentType.OPENAI_FUNCTIONS, verbose=True,)agent.run("Create Main.java...")Short, but: what’s sent to the model? What’s the exact format? How does the loop terminate? All of that is inside initialize_agent. To untangle it you have to read LangChain’s docs and source code.
With bare Ollama, ollama-demo-3-agent-java/agent_java.py fits in 30 useful lines (the loop from chapter 03). You see:
- the exact
messageslist; - the direct call to
client.chat(...); - the raw
response.messageyou can print; - the explicit Python loop that retries.
It’s printable on a slide. It’s readable by a student who’s just starting.
Comparison table
Section titled “Comparison table”| Criterion | Bare Ollama (our choice) | LangChain |
|---|---|---|
| Lines of code for a simple agent | ~30 | ~10 (but ~10 000 hidden) |
| Visibility on every message | Full | Partial (verbose) |
| Learning curve | Low | Medium to high |
| Switch provider | Manual | One line |
| RAG, vector store, memory | DIY | Included |
| Tool calling | Direct (pass the Python function) | Via Tool.from_function or decorators |
| Fine control on the prompt | Total | Possible but indirect |
| Target audience | Learning, prototyping, tutorials | Multi-provider production |
| pip dependency count | 1 (ollama) | ~30+ |
Ollama supports tool calling natively
Section titled “Ollama supports tool calling natively”The point that makes LangChain unnecessary in this course: since July 2024, Ollama supports tool calling in an OpenAI-compatible format. The ollama-python SDK exposes the same API. You write:
from ollama import Client
client = Client(host="http://127.0.0.1:11434")response = client.chat( model="llama3.1:8b", messages=[...], tools=[my_python_function_1, my_python_function_2],)print(response.message.tool_calls) # structured listAnd that’s it. No intermediate framework. The SDK does:
- introspection of Python signatures to generate the JSON-schema;
- sending the list to Ollama;
- parsing the response into
ToolCallobjects.
It’s the same primitive that LangChain wraps.
When to switch to LangChain (or another framework)
Section titled “When to switch to LangChain (or another framework)”You’ll start benefiting from LangChain (or llama-index, smolagents, autogen, etc.) the day you:
- want to run RAG over 10 000 documents;
- want to route smartly between 3 providers based on cost;
- want agents that hand off to each other with a standard protocol;
- want long-term memory persisted in a database.
But by then, you’ll understand what those frameworks do because you’ll have already coded them by hand in demo 3.
Key takeaways
Section titled “Key takeaways”- LangChain is useful for complex multi-provider production projects.
- To learn what an agent is, LangChain hides exactly what we want to see.
- Ollama supports tool calling natively since 2024; the
ollama-pythonSDK makes the API trivial to use. - The whole repo uses
from ollama import chat, Clientand nothing else on the LLM side. One dependency, zero hidden abstraction. - If you understand our agent loop, you’ll understand what LangChain does when you get there.