← Back to Docs
Recipe

LangChain Primer

Chain LLM calls, tools, and memory into composable pipelines that actually ship.

LangChain is the open-source framework that turns raw model outputs into structured, multi-step workflows. Instead of one-shot prompting, you build chains — sequences where each step feeds the next, with optional tool use, retrieval, and state.

Core Concepts

  • LLMChain— prompt template + model + optional output parser
  • Tools— functions the model can call (search, calc, API)
  • Agents— decide which tool to use and when, loop until done
  • Memory— persist conversation state across turns
  • RAG— retrieval-augmented generation: vector DB → context → answer

Minimal Chain

from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMChain

prompt = PromptTemplate(
    input_variables=["topic"],
    template="Explain {topic} in two sentences."
)
chain = LLMChain(llm=OpenAI(), prompt=prompt)
print(chain.run("zero-knowledge proofs"))

Agent with Tools

from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI

def search(query: str) -> str:
    return f"Results for: {query}"

tools = [Tool(name="Search", func=search, description="web search")]
agent = initialize_agent(tools, OpenAI(), agent="zero-shot-react-description")
agent.run("What is the capital of France?")

LangChain shines when you need the model to decide what to do next — call an API, query a database, or ask for clarification — rather than just emit text. Pair it with LangSmith for tracing and LangServe for deployment.