---
name: langchain-agent-builder
description: "Build LangChain agents — chains, tools, memory, RAG, LangGraph state machines, eval framework. Production LangChain (Python + JS): LCEL (LangChain Expression Language), tool calling, conversation memory, RAG pipelines, LangGraph for stateful agents, evals with LangSmith, and the patterns that move from prototype to scale."
license: MIT
---

# LangChain Agent Builder

Scaffolds production-grade LangChain applications in both Python and JavaScript/TypeScript: composable chains, tool-calling agents, conversation memory, RAG pipelines, and stateful LangGraph workflows. It's for developers who've outgrown a single `ChatOpenAI().invoke()` call and need the structural patterns — retries, streaming, persistence, evaluation — that survive contact with real traffic.

## When to use it

- You're wiring an LLM to external tools (search, SQL, internal APIs) and want reliable tool-calling instead of brittle string parsing.
- You need retrieval over your own documents and want a vectorstore-backed RAG chain with proper chunking and citation.
- A linear chain isn't enough — you need branching, loops, human-in-the-loop, or multi-agent handoffs, which means LangGraph.
- You're moving a notebook prototype to a service and need streaming, async, and traceable runs.

## What it does

Generates idiomatic **LCEL** pipelines using the `|` operator over `Runnable` primitives (`RunnablePassthrough`, `RunnableParallel`, `RunnableLambda`) so chains stream and batch for free. For agents it uses `create_tool_calling_agent` / `AgentExecutor` and the `@tool` decorator, binding schemas via `.bind_tools()`. Memory is handled with `RunnableWithMessageHistory` and a chat history store. RAG scaffolds a loader → `RecursiveCharacterTextSplitter` → embeddings → vectorstore (Chroma, FAISS, pgvector) → `as_retriever()` flow. For stateful agents it builds `StateGraph` with typed `TypedDict` state, `add_node`/`add_edge`/`add_conditional_edges`, and a checkpointer (`MemorySaver` or `SqliteSaver`) for persistence and resumption. Evaluation hooks into **LangSmith** datasets and `evaluate()` with custom and off-the-shelf evaluators.

## Example

```python
from langgraph.graph import StateGraph, END
from langgraph.checkpoint.memory import MemorySaver
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages

class State(TypedDict):
    messages: Annotated[list, add_messages]

g = StateGraph(State)
g.add_node("agent", call_model)        # binds tools, returns AIMessage
g.add_node("tools", tool_node)
g.add_conditional_edges("agent", route_tools, {"tools": "tools", END: END})
g.add_edge("tools", "agent")
g.set_entry_point("agent")

app = g.compile(checkpointer=MemorySaver())
app.invoke({"messages": [("user", "weather in Berlin?")]},
           config={"configurable": {"thread_id": "1"}})
```

## Tips & gotchas

- Set `LANGCHAIN_TRACING_V2=true` and `LANGCHAIN_API_KEY` before debugging — most "why did the agent loop" questions are answered instantly in the LangSmith trace.
- Always pass a `thread_id` in `config` when using a checkpointer; without it, state won't persist across calls.
- Prefer `.astream_events(version="v2")` for token-level streaming through nested chains — `.stream()` alone won't surface inner LLM tokens.
- Pin `langchain-core`; the v0.1→0.3 splits moved integrations into `langchain-openai`, `langchain-community`, etc.

## FAQ

**LangChain agents vs. LangGraph — which?** Use `AgentExecutor` for simple ReAct-style tool loops. Reach for LangGraph the moment you need cycles, persistence, human approval steps, or multiple cooperating agents.

**Python or JS?** Both are scaffolded. The JS package (`@langchain/core`, `@langchain/langgraph`) mirrors the Python API closely, though some community integrations lag.

**Do I need LangSmith?** No — it's optional, but the eval and tracing patterns assume it. Everything else runs without an account.
