agentleFS
svngokusvngoku/coding-agents-skills/skills/langchain/SKILL.md

langchain

Skill12 starsChanged 23 days agoMITView on GitHub

Build AI agents with LangChain v1 and LangGraph in Python. Use this skill whenever the user mentions LangChain, LangGraph, create_agent, init_chat_model, @tool, ToolRuntime, agent middleware, human-in-the-loop, structured output, MCP tools (langchain.mcp, MCPAdapter), RAG, multi-agent handoffs or subagents, checkpointers, Deep Agents (create_deep_agent) or Managed Deep Agents (mda, define_deep_agent). Also trigger for migrating from create_react_agent, LLMChain, AgentExecutor or langchain-mcp-adapters, stream_events, SummarizationMiddleware, and langchain-classic.

  • Installs packages

11 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use analytics-metrics, aws-account-management and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • hoodiniSkill

    analytics-metrics

    Build data visualization and analytics dashboards. Use when creating charts, KPI displays, metrics dashboards, or data visualization components. Triggers on analytics, dashboard, charts, metrics, KPI, data visualization, Recharts.

    Also in 3 projects that use this

  • hoodiniSkill

    aws-account-management

    Manage AWS accounts, organizations, IAM, and billing. Use when setting up AWS Organizations, managing IAM policies, controlling costs, or implementing multi-account strategies. Triggers on AWS Organizations, AWS IAM, AWS billing, Cost Explorer, SCPs, multi-account, AWS SSO, Identity Center.

    Also in 3 projects that use this

  • carapace-shSkill

    bun

    Use when working with the bun CLI — Bun's all-in-one JavaScript runtime and toolkit. Covers the CLI command reference (install, add, remove, run, build, test, x, create, init, pm, upgrade, link, unlink), bunfig.toml configuration, environment variables, the package manager and lockfile, the test runner, the bundler, the Bun runtime API (Bun.serve, Bun.file, Bun.spawn, Bun.write, hashing, transpiler), the Bun shell ($ template literal), module resolution and loaders, and globals. Triggers on: "bun", "bunx", "bun install", "bun add", "bun remove", "bun run", "bun build", "bun test", "bun x", "bun create", "bun init", "bun pm", "bun upgrade", "bun link", "bun unlink", "bunfig.toml", "bun.lock", "Bun.serve", "Bun.file", "Bun.write", "Bun.spawn", "Bun.hash", "Bun.password", "bun:test", "Bun.build", "Bun.Transpiler", "bun shell", "$ template", "import.meta", "JavaScriptCore", "Bun.serve", "HTMLRewriter", "Bun.SQL", "Bun.RedisClient", "Bun.Glob", "Bun.plugin", "bun:ffi".

    Also in 4 projects that use this

  • imehrSkill

    railway

    Railway.com deployment and management - deployment, logs, migrations, troubleshooting, monorepo strategies, security, and CLI reference. Use when deploying to Railway, configuring services, managing environment variables, or debugging deployment issues.

    Also in 3 projects that use this

  • +8 more from hoodini

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 1 is distinctive, and 4 are very specific to this project.

  • 0

    Common

    Most of the 8 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use event streaming (version="v3") for apps and UIs.

    line 219
  2. Very specific:

    Global state in tools: read per-user data from ToolRuntime[Context], not globals, so tools stay testable and safe in concurrent runs

    line 367
  3. Very specific:

    Hand-rolled hook loops: use middleware (SummarizationMiddleware, ModelFallbackMiddleware, ToolRetryMiddleware, limits) instead of wrapping …

    line 369
  4. Very specific:

    Letting raw tool exceptions reach the model or user: use ToolErrorMiddleware(on_error=...) or ToolRetryMiddleware, and return the exception …

    line 370
  5. Distinctive:

    Stuffing whole documents into context: use the retrieval patterns in references/retrieval.md

    line 374

“Common” is measured: kept by at least 6 in 10 of the 8 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

LangChain Skill

Build production AI agents with LangChain v1 (create_agent on top of LangGraph), its middleware system, built-in MCP support and the LangGraph runtime. Verified against langchain 1.4.6, langchain-core 1.6, langgraph 1.2 and deepagents 0.7 (Python 3.10+; deepagents needs 3.11+).

Quick Reference

TopicFile
Built-in and custom middlewarereferences/middleware.md
LangGraph graphs, persistence, durability, streamingreferences/langgraph.md
Subagents, handoffs, skills, routersreferences/multi-agent.md
Deep Agents harness and Managed Deep Agents (mda)references/deep-agents.md
RAG, embeddings, vector stores, retrieversreferences/retrieval.md

Installation

pip install -U langchain langgraph langchain-anthropic   # or langchain-openai, langchain-google-genai, ...
pip install -U "langchain[mcp]"                          # built-in MCP client (langchain.mcp, beta)
pip install -U deepagents                                # optional: planning + filesystem + subagents harness
pip install -U langchain-classic                         # only for legacy chains/retrievers

Quick Agent

from langchain.agents import create_agent

def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"Sunny in {city}!"

agent = create_agent(
    model="anthropic:claude-sonnet-5",
    tools=[get_weather],
    system_prompt="You are a helpful assistant.",
)

result = agent.invoke({"messages": [{"role": "user", "content": "Weather in SF?"}]})
print(result["messages"][-1].content)

create_agent returns a compiled LangGraph graph, so invoke, stream, stream_events, checkpointers, stores and interrupts all work on it. Plain functions with type hints and a docstring are accepted as tools; use @tool when you need a custom name, schema or ToolRuntime.

Model Configuration

from langchain.chat_models import init_chat_model

model = init_chat_model(
    "anthropic:claude-sonnet-5",   # "provider:model"; e.g. "openai:gpt-5.5", "google_genai:gemini-3.6-flash"
    temperature=0, timeout=30, max_tokens=2000, max_retries=2,
)

model.profile["max_input_tokens"]    # model capabilities (from models.dev): context size, tool calling, modalities

Pass either the string or the model instance as model= to create_agent. system_prompt accepts a str or a SystemMessage (useful for content blocks such as provider cache-control markers).

Tools

The docstring becomes the description the model sees, and type hints become the argument schema:

from dataclasses import dataclass
from langchain.tools import tool, ToolRuntime

@tool
def search(query: str, limit: int = 10) -> str:
    """Search the order database. Returns up to `limit` matching records."""
    return f"Found {limit} results for '{query}'"

@dataclass
class Context:
    user_id: str

@tool
def get_user_data(runtime: ToolRuntime[Context]) -> str:
    """Get the current user's profile."""
    return f"Data for {runtime.context.user_id}"

ToolRuntime is injected and hidden from the model. It exposes state (messages and custom fields), context (immutable per-run data passed as agent.invoke(..., context=Context(...))), store (long-term memory), stream_writer (custom stream updates), tool_call_id and config. config and runtime are reserved argument names. Finer-grained injectables also live in langchain.tools: InjectedState, InjectedStore, InjectedToolCallId, InjectedToolArg.

Memory

from langgraph.checkpoint.memory import InMemorySaver
from langgraph.store.memory import InMemoryStore

agent = create_agent(
    model="anthropic:claude-sonnet-5",
    tools=[search],
    checkpointer=InMemorySaver(),   # short-term: per-thread conversation state
    store=InMemoryStore(),          # long-term: cross-thread memory via runtime.store
)

config = {"configurable": {"thread_id": "conversation-1"}}
agent.invoke({"messages": [{"role": "user", "content": "Hi, I'm Ana"}]}, config=config)

In production use PostgresSaver/AsyncPostgresSaver or SqliteSaver (references/langgraph.md), and keep long threads in budget with SummarizationMiddleware.

Structured Output

from pydantic import BaseModel
from langchain.agents.structured_output import ToolStrategy, ProviderStrategy

class Response(BaseModel):
    answer: str
    confidence: float | None = None

agent = create_agent(model="anthropic:claude-sonnet-5", tools=[search],
                     response_format=ToolStrategy(Response))
result = agent.invoke({"messages": [{"role": "user", "content": "..."}]})   # result["structured_response"]: Response
response_formatBehavior
Response (bare type)Auto: provider-native JSON schema when the model profile supports it, else tool calling
ToolStrategy(Response, handle_errors=True)Structured output via an artificial tool call; retries on validation errors; works with any tool-calling model
ProviderStrategy(Response, strict=True)Provider-native structured output (OpenAI, Anthropic, Gemini, ...)

Schemas can be Pydantic models, dataclasses, TypedDicts or JSON schema dicts. Outside agents, use model.with_structured_output(Response).

Middleware

Middleware is the v1 extension point that replaces pre/post hooks. Built-ins live in langchain.agents.middleware:

from langchain.agents import create_agent
from langchain.agents.middleware import (
    SummarizationMiddleware, HumanInTheLoopMiddleware, ModelFallbackMiddleware,
    ToolCallLimitMiddleware, PIIMiddleware,
)
from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent(
    model="anthropic:claude-sonnet-5",
    tools=[search, send_email],
    checkpointer=InMemorySaver(),   # required for interrupts
    middleware=[
        SummarizationMiddleware(model="openai:gpt-5.4-mini", trigger=("fraction", 0.8), keep=("messages", 20)),
        ModelFallbackMiddleware("openai:gpt-5.5"),
        ToolCallLimitMiddleware(tool_name="search", run_limit=5),
        PIIMiddleware("email", strategy="redact", apply_to_input=True),
        HumanInTheLoopMiddleware(interrupt_on={"send_email": {"allowed_decisions": ["approve", "edit", "reject"]}}),
    ],
)

Custom middleware uses decorators (before_agent, before_model, after_model, after_agent, wrap_model_call, wrap_tool_call, dynamic_prompt) or an AgentMiddleware subclass:

from langchain.agents.middleware import dynamic_prompt, wrap_tool_call, ModelRequest

@dynamic_prompt
def tiered_prompt(request: ModelRequest) -> str:
    return f"You are a support agent. Customer tier: {request.runtime.context.tier}."

@wrap_tool_call
def audit_tool_calls(request, handler):
    print("calling", request.tool_call["name"], request.tool_call["args"])
    return handler(request)

Full catalogue (retries, tool selection, context editing, to-do lists, shell, emulators) and patterns (dynamic models, dynamic tools, jumps) are in references/middleware.md.

Human-in-the-Loop

With HumanInTheLoopMiddleware and a checkpointer, the run pauses before the listed tools. Resume with one decision per pending action:

from langgraph.types import Command

config = {"configurable": {"thread_id": "t1"}}
result = agent.invoke({"messages": [{"role": "user", "content": "Email Bob the report"}]},
                      config=config, version="v2")
while result.interrupts:
    actions = result.interrupts[0].value["action_requests"]
    for action in actions:
        print("Pending:", action["name"], action["args"])
    decisions = [{"type": "approve"} for _ in actions]   # one decision per action, in order
    result = agent.invoke(Command(resume={"decisions": decisions}), config=config, version="v2")

Decision types: {"type": "approve"}, {"type": "edit", "edited_action": {"name": ..., "args": {...}}}, {"type": "reject", "message": "why"}, and {"type": "respond", "message": "..."}. With version="v2", invoke returns a GraphOutput (.value, .interrupts) instead of a plain dict.

Streaming

Use event streaming (version="v3") for apps and UIs. It returns a run object with typed projections:

stream = agent.stream_events({"messages": [{"role": "user", "content": "Weather in SF?"}]}, version="v3")
for kind, item in stream.interleave("messages", "tool_calls"):
    if kind == "messages":
        for token in item.text:
            print(token, end="", flush=True)
    else:
        print(f"\n[tool] {item.tool_name}({item.input})")
        for delta in item.output_deltas:   # drain first: item.output is None until the tool finishes
            print(delta, end="", flush=True)
        print(f"\n[result] {item.output}")
final_state = stream.output

Event streaming is experimental in LangGraph 1.2 (it emits a LangChainBetaWarning). Classic agent.stream(input, stream_mode=["updates", "messages", "custom"]) still works; the model node is "model".

MCP Integration

LangChain 1.4 ships a built-in MCP client, langchain.mcp (beta, built on FastMCP). It replaces langchain-mcp-adapters' MultiServerMCPClient:

from langchain.agents import create_agent
from langchain.mcp import MCPAdapter

config = {"mcpServers": {
    "math": {"command": "python", "args": ["/path/to/math_server.py"]},             # stdio
    "api": {"url": "https://api.example.com/mcp", "headers": {"Authorization": "Bearer TOKEN"}},
}}

async def build_agent():
    async with MCPAdapter(config) as adapter:     # or MCPAdapter("https://..."), MCPAdapter(Path("server.py"))
        tools = await adapter.list_tools()        # multi-server tools are prefixed: math_..., api_...
    return create_agent("anthropic:claude-sonnet-5", tools)
  • A plain string target must be an http(s) URL; reach local servers with pathlib.Path(...) or a config.
  • Tools stay usable after the async with block exits. They are async, so run the agent with ainvoke/astream.
  • Server elicitation requests become LangGraph interrupt()s, so add a checkpointer.
  • Interceptors are gone: use @wrap_tool_call middleware (MCP provenance is in request.tool.metadata["mcp"]).

State Updates from Tools

Return a Command to update state or control flow:

from langchain.messages import RemoveMessage, ToolMessage
from langchain.tools import tool, ToolRuntime
from langgraph.graph.message import REMOVE_ALL_MESSAGES
from langgraph.types import Command

@tool
def clear_history() -> Command:
    """Clear the conversation history."""
    return Command(update={"messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES)]})

@tool
def set_language(language: str, runtime: ToolRuntime) -> Command:
    """Remember the user's preferred language."""
    return Command(update={
        "language": language,   # custom field: needs state_schema= (a TypedDict extending AgentState)
        "messages": [ToolMessage(f"Language set to {language}", tool_call_id=runtime.tool_call_id)],
    })

Deep Agents

deepagents wraps create_agent with a virtual filesystem, a task tool for subagents, summarization, and optional skills, memory files, permissions and sandboxes, for long multi-step tasks. Managed Deep Agents (managed-deepagents, CLI mda) deploys the same harness to LangSmith from a project directory (agent.py with define_deep_agent, instructions.md, memory.py, sandbox/, schedules/). Both are covered in references/deep-agents.md.

from deepagents import create_deep_agent

agent = create_deep_agent(
    model="anthropic:claude-sonnet-5",
    tools=[internet_search],
    system_prompt="You are an expert researcher. Write the final report to /report.md.",
    subagents=[{"name": "critic", "description": "Reviews drafts", "system_prompt": "You are a strict reviewer."}],
)

Full Production Agent

from dataclasses import dataclass
from langchain.agents import create_agent
from langchain.agents.middleware import SummarizationMiddleware, ToolRetryMiddleware
from langchain.agents.structured_output import ToolStrategy
from langchain.chat_models import init_chat_model
from langchain.tools import tool, ToolRuntime
from langgraph.checkpoint.memory import InMemorySaver

@dataclass
class Context:
    user_id: str

@tool
def search_orders(query: str, runtime: ToolRuntime[Context]) -> str:
    """Search the current user's orders by keyword, date or status."""
    return f"Orders for {runtime.context.user_id}: {query}"

@dataclass
class Response:
    answer: str
    order_ids: list[str] | None = None

agent = create_agent(
    model=init_chat_model("anthropic:claude-sonnet-5", temperature=0),
    system_prompt="You are a customer service agent.",
    tools=[search_orders],
    context_schema=Context,
    response_format=ToolStrategy(Response),
    middleware=[ToolRetryMiddleware(max_retries=2), SummarizationMiddleware("openai:gpt-5.4-mini", trigger=("tokens", 4000))],
    checkpointer=InMemorySaver(),
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "Find my recent orders"}]},
    config={"configurable": {"thread_id": "session-1"}},
    context=Context(user_id="user123"),
)
print(result["structured_response"])

Migrating to v1

Old (0.x / pre-1.4)Current
langgraph.prebuilt.create_react_agentlangchain.agents.create_agent
prompt=system_prompt= (static) or @dynamic_prompt middleware
pre_model_hook / post_model_hook@before_model / @after_model middleware
config["configurable"] for user datacontext_schema= + invoke(..., context=...)
AgentExecutor, LLMChain, RetrievalQA, langchain.retrieverscreate_agent, or langchain_classic.* for legacy code
langchain.text_splitterlangchain_text_splitters
langchain_mcp_adapters.client.MultiServerMCPClientlangchain.mcp.MCPAdapter
MCP tool_interceptors@wrap_tool_call middleware
Prompted structured outputToolStrategy / ProviderStrategy
message.content parsing for reasoning/citationsmessage.content_blocks (standard blocks)

Anti-Patterns to Avoid

  • Tools without type hints or docstrings: the docstring is the tool description the model sees; a missing or vague one means the agent misuses the tool
  • Global state in tools: read per-user data from ToolRuntime[Context], not globals, so tools stay testable and safe in concurrent runs
  • No checkpointer, then wondering why the agent forgets: multi-turn agents, human-in-the-loop and MCP elicitation all need a checkpointer plus a stable thread_id
  • Hand-rolled hook loops: use middleware (SummarizationMiddleware, ModelFallbackMiddleware, ToolRetryMiddleware, limits) instead of wrapping agent.invoke in custom retry/trim code
  • Letting raw tool exceptions reach the model or user: use ToolErrorMiddleware(on_error=...) or ToolRetryMiddleware, and return the exception type rather than internal details
  • Importing removed modules: langchain.vectorstores, langchain.document_loaders, langchain.text_splitter, langchain.retrievers and LLMChain no longer exist in langchain v1; use provider packages, langchain_core, langchain_text_splitters or langchain_classic
  • Starting new code on langchain-mcp-adapters or create_react_agent: both are superseded by langchain.mcp and create_agent
  • Passing a local script path as a string to MCPAdapter: strings must be URLs; use Path(...) or an mcpServers config for stdio servers
  • Stuffing whole documents into context: use the retrieval patterns in references/retrieval.md

When to Use / Not Use

Use this skill when:

  • Building agents with LangChain v1 or LangGraph in Python
  • Adding tools, memory, middleware, human approval, MCP servers or structured output to an LLM app
  • Implementing RAG, subagents, handoffs, routers, Deep Agents (self-hosted or managed) or stateful workflows
  • Migrating 0.x code (create_react_agent, AgentExecutor, chains, langchain-mcp-adapters)

Do NOT use this skill when:

  • A single stateless LLM call is all you need; the plain provider SDK is lighter
  • The project standardizes on another agent framework; follow the project's stack
  • The agent is code-first and benefits from the smaller SmolAgents execution model; see the smolagents skill

Key Links

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.