LangChain Building Blocks: Chains, Tools and Agent Control
What LangChain actually abstracts, when an agent loop is the wrong choice, and how to keep retrieval, memory and token cost under control in production.
LangChain is a framework for composing applications around large language models. It does not make a model smarter. What it provides is a set of interfaces for the parts that surround the model: prompt construction, output parsing, retrieval, tool calling, and multi step control flow. Knowing which of those you actually need is most of the work.
Composition Before Agents
The base unit is a chain: a deterministic sequence where you decide what happens in what order. A prompt template fills variables into a prompt, the model returns text or structured output, an output parser turns that into a typed object, and the next step consumes it. Control flow is yours.
An agent inverts that. The model decides which tool to call and when to stop, looping until it produces a final answer. That flexibility costs determinism, latency, and tokens, and it introduces failure modes a chain does not have: loops that never terminate, tools called with malformed arguments, and reasoning that drifts off task.
The practical rule is to start with an explicit chain. Reach for an agent only when the sequence of steps genuinely cannot be known in advance. Many production systems that began as agents end up as chains with one conditional branch.
When a loop really is required, the next decision is which runtime executes it. A linear executor and a checkpointed graph runtime handle failure, resumption and human approval very differently, and that choice is compared in LangChain vs LangGraph vs LlamaIndex. The failure modes an agent loop introduces, and the configuration that contains each one, are catalogued in LangChain agent errors.
Retrieval Is a Data Problem
Retrieval augmented generation attaches relevant context to a prompt so the model answers from your data instead of its parameters. The framework makes the plumbing easy, which conceals the fact that quality is almost entirely determined by decisions you make before any model call.
Chunking is the first of those. Chunks that are too small lose the context needed to interpret them. Chunks that are too large dilute the embedding and waste prompt space. Splitting on document structure such as headings and sections usually beats splitting on a fixed character count, because it keeps semantically coherent material together.
Embeddings must be consistent. The same model has to embed both documents and queries, and changing embedding models means reindexing the entire corpus. Storing the model identity alongside the index saves a painful debugging session later.
Pure vector similarity retrieves things that are topically close but not necessarily correct. Combining keyword search with vector search, then reranking the merged candidates, usually recovers the exact matches that embeddings miss, such as identifiers, error codes, and proper nouns. Always keep source metadata on each chunk so answers can cite where they came from.
Each of those decisions is unpacked stage by stage, with the parameters worth setting deliberately, in the LangChain RAG pipeline walkthrough.
Memory, Context and Cost
Conversational memory is just text you choose to resend. Every turn you keep in the window is paid for on every subsequent call. Summarizing older turns, or retrieving only the relevant history, keeps cost bounded as conversations grow.
Token cost scales with prompt size, so the largest savings come from sending less: fewer retrieved chunks, tighter system prompts, and structured output instead of prose you then have to parse. Measure where tokens go before optimizing, because the intuition is usually wrong.
For a first estimate before any of that is built, the token and agent cost sizer multiplies step count, prompt size and retrieved context against published model rates to show the cost per thousand runs.
Evaluate and Trace
Prompt changes that look better on one example often regress others. Build a fixed evaluation set of representative inputs with expected behavior, and rerun it after every change. Enable tracing so you can see the exact prompt sent, the tools called, and the intermediate outputs. Debugging an LLM application without visibility into the assembled prompt is guesswork.
Sources
Related
How to Use LangChain with Ollama: Local Chat, Tools, Structured Output and Embeddings
Wire ChatOllama and OllamaEmbeddings to a local Ollama server: install, the context window and keep_alive settings that decide whether it holds up, bind_tools, with_structured_output, and the failure modes to expect.
LangChain Agent Errors: Loops, Tools, Parsing
Why LangChain agents loop forever, skip tools, pass bad arguments or fail to parse output, and the configuration changes that fix each symptom.
LangChain vs LangGraph vs LlamaIndex Compared
LangChain, LangGraph and LlamaIndex solve different problems. A side-by-side comparison of scope, state model, retrieval depth and which one to pick.