What Is LangChain?
LangChain is a Python framework for building applications on top of LLMs. It provides abstractions for call chains, memory, tools and agents.
Installation
python3 -m venv langchain-env
source langchain-env/bin/activate
pip install langchain langchain-community langchain-ollama
pip install python-dotenv chromadb Connecting to Ollama
from langchain_ollama import OllamaLLM
llm = OllamaLLM(model="llama3.2", base_url="http://localhost:11434")
response = llm.invoke("Tell me about the advantages of a VDS")
print(response) A Simple Chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_ollama import OllamaLLM
prompt = ChatPromptTemplate.from_messages([
("system", "You are a hosting assistant."),
("user", "{question}")
])
llm = OllamaLLM(model="llama3.2")
chain = prompt | llm
result = chain.invoke({"question": "How does a VDS differ from shared hosting?"})
print(result) An Agent with Tools
from langchain.agents import create_react_agent, AgentExecutor
from langchain.tools import tool
from langchain_ollama import OllamaLLM
from langchain import hub
@tool
def get_server_info(query: str) -> str:
# Returns information about VDS plans
return "VDS Start 2GB RAM from 299 RUB/month"
llm = OllamaLLM(model="llama3.2", temperature=0)
tools = [get_server_info]
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = executor.invoke({"input": "Which plan do I need for an AI agent?"})
print(result["output"]) Common Errors
Most LangChain errors come not from chain logic but from the fast-moving API: modules move between packages faster than online documentation can keep up.
ImportError: cannot import name 'OllamaLLM' from 'langchain_community.llms'
The Ollama integration was moved out of langchain_community into a separate langchain-ollama package. Older examples online import the class from the community module, but the current OllamaLLM class must be imported from langchain_ollama, as shown above. Installing only langchain without langchain-ollama is the second common cause of the same error.
Agent stopped due to iteration limit or time limit.
This is the exact response AgentExecutor returns when the agent cannot produce a final answer within the allotted number of steps (15 iterations by default). It is not a crash but a safeguard against infinite loops. If an agent regularly hits the limit, the problem is usually in the tool's description: the agent does not understand when it is time to stop and return a Final Answer.
Key LCEL Operators
| Syntax | What it does |
|---|---|
| prompt | llm | Passes the prompt's output into the llm |
| chain.invoke(dict) | Synchronous chain call with parameters |
| chain.stream(dict) | Streams tokens as they are generated |
| chain.batch([dict, ...]) | Processes several requests in parallel |
When LangChain Is Not a Good Fit
- The task is a single model call without chains or memory: calling the Ollama API directly is shorter and avoids extra dependencies.
- Your team needs a stable API across upgrades — LangChain changes its interfaces noticeably more often than most libraries at its level.
- You need a ready-made role-based agent crew out of the box — CrewAI with its role abstraction fits this better.
- Per-request performance is critical — LangChain's abstraction layers add overhead compared to calling APIs directly.
Verifying the Chain and Agent Work
python3 -c "import langchain, langchain_ollama; print(langchain.__version__)"
ollama list First, make sure both packages are installed and compatible. Then run the simple chain without an agent (section above) — if it returns coherent text, the LLM and connection are set up correctly. Only then move on to the agent: if the ReAct agent returns "Agent stopped due to iteration limit" while the simple chain works fine, the problem is in the tool description, not the model connection.
Another point that is often missed: LCEL chains do not cache intermediate results between calls by default. If the same LLM request repeats across different branches of a chain, it will be executed again each time. For repeated sub-requests, explicitly wire up caching at the LangChain level or store intermediate results yourself, especially if the model runs slowly on CPU.