langchain

LangChain Python package — new create_agent() factory (builds a LangGraph agent from a model string + tools + middleware), plus a comprehensive middleware system covering HITL, PII redaction, model fallback, rate limiting, auto-summarization, context editing, shell tools, and todo planning.

11 stars

Best use case

langchain is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

LangChain Python package — new create_agent() factory (builds a LangGraph agent from a model string + tools + middleware), plus a comprehensive middleware system covering HITL, PII redaction, model fallback, rate limiting, auto-summarization, context editing, shell tools, and todo planning.

Teams using langchain should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/langchain/SKILL.md --create-dirs "https://raw.githubusercontent.com/enuno/claude-command-and-control/main/skills/langchain/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/langchain/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How langchain Compares

Feature / AgentlangchainStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

LangChain Python package — new create_agent() factory (builds a LangGraph agent from a model string + tools + middleware), plus a comprehensive middleware system covering HITL, PII redaction, model fallback, rate limiting, auto-summarization, context editing, shell tools, and todo planning.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# LangChain Skill

Expert assistance for the `langchain` Python package's new agent API: `create_agent()` — a high-level factory that builds a LangGraph-backed agent with composable middleware for cross-cutting concerns (HITL, PII, fallback, limits, summarization, etc.).

**Install**: `pip install -U langchain`

Reference: `references/api.md` (500 KB — full API reference).

## When to Use This Skill

Activate when:
- **Creating an agent with `create_agent()`** — using model strings, tools, and middleware
- **Using init_chat_model strings** — e.g. `"anthropic:claude-sonnet-4-5"` as the `model` param
- **Adding HITL approval gates** — using `HumanInTheLoopMiddleware` per tool
- **Redacting PII** — using `PIIMiddleware` to detect/redact/mask/hash PII in input or output
- **Adding model fallback** — using `ModelFallbackMiddleware` for automatic failover
- **Limiting model or tool calls** — using `ModelCallLimitMiddleware` or `ToolCallLimitMiddleware`
- **Auto-summarizing long conversations** — using `SummarizationMiddleware` on token/message threshold
- **Retrying failed calls** — using `ModelRetryMiddleware` or `ToolRetryMiddleware`
- **Structured agent output** — using `response_format` param on `create_agent()`
- **Composing multiple middleware** — stacking middleware in the `middleware` list

## Quick Reference

### create_agent() — minimal agent

```python
from langchain.agents import create_agent
from langchain_core.tools import tool

@tool
def check_weather(location: str) -> str:
    """Return the weather forecast for a location."""
    return f"It's sunny and 22°C in {location}."

# model can be a string (uses init_chat_model) or a BaseChatModel instance
agent = create_agent(
    model="anthropic:claude-sonnet-4-6",   # or "openai:gpt-4o", "google:gemini-2.0-flash"
    tools=[check_weather],
    system_prompt="You are a helpful assistant.",
)

# Returns a CompiledStateGraph (standard LangGraph interface)
for chunk in agent.stream(
    {"messages": [{"role": "user", "content": "What's the weather in Paris?"}]},
    stream_mode="updates",
):
    print(chunk)
```

### HumanInTheLoopMiddleware — per-tool approval gates

```python
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langchain_core.tools import tool

@tool
def delete_file(path: str) -> str:
    """Delete a file from the filesystem."""
    import os; os.remove(path); return f"Deleted {path}"

@tool
def read_file(path: str) -> str:
    """Read file contents."""
    return open(path).read()

hitl = HumanInTheLoopMiddleware(
    interrupt_on={
        "delete_file": True,    # all decisions: approve/edit/reject/respond
        "read_file": False,     # auto-approve (no interrupt)
        # "write_file": InterruptOnConfig(approve=True, reject=True, description="Approve write?")
    }
)

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=[delete_file, read_file],
    middleware=[hitl],
    checkpointer=...,   # required for HITL interrupts
)

# Resume after interrupt (same as LangGraph interrupt pattern)
from langchain_core.messages import HumanMessage
from langgraph.types import Command
agent.invoke(Command(resume="approve"), config={"configurable": {"thread_id": "1"}})
```

### PIIMiddleware — detect and handle PII

```python
from langchain.agents import create_agent
from langchain.agents.middleware import PIIMiddleware

# Redact emails and credit cards from user input
pii = PIIMiddleware(
    "email",
    strategy="redact",        # "block" | "redact" | "mask" | "hash"
    apply_to_input=True,      # scan user messages
    apply_to_output=False,    # don't scan agent responses
    apply_to_tool_results=False,
)

# Stack multiple PII middleware
agent = create_agent(
    model="openai:gpt-4o",
    tools=[],
    middleware=[
        PIIMiddleware("email", strategy="redact"),
        PIIMiddleware("credit_card", strategy="mask"),    # ****-****-****-1234
        PIIMiddleware("ip", strategy="hash"),
    ],
)
```

### ModelFallbackMiddleware — automatic model failover

```python
from langchain.agents import create_agent
from langchain.agents.middleware import ModelFallbackMiddleware

fallback = ModelFallbackMiddleware(
    "openai:gpt-4o-mini",                    # try first on error
    "anthropic:claude-haiku-4-5-20251001",   # then this
    # additional fallbacks...
)

agent = create_agent(
    model="openai:gpt-4o",    # primary model
    tools=[...],
    middleware=[fallback],
)
```

### SummarizationMiddleware — auto-compress long conversations

```python
from langchain.agents import create_agent
from langchain.agents.middleware import SummarizationMiddleware

summarizer = SummarizationMiddleware(
    model="openai:gpt-4o-mini",    # model to generate summaries
    trigger=[
        ("fraction", 0.8),         # trigger at 80% of model's context window
        ("messages", 100),         # or at 100 messages, whichever first
    ],
    keep=("messages", 20),         # keep most recent 20 messages after summary
)

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=[...],
    middleware=[summarizer],
)
```

### ModelCallLimitMiddleware — rate limit model calls

```python
from langchain.agents import create_agent
from langchain.agents.middleware import ModelCallLimitMiddleware

limiter = ModelCallLimitMiddleware(
    thread_limit=50,        # max model calls across all runs in a thread
    run_limit=10,           # max model calls in a single run
    exit_behavior="end",    # "end" (graceful) | "error" (raise exception)
)

agent = create_agent(
    model="openai:gpt-4o",
    tools=[...],
    middleware=[limiter],
)
```

### Structured agent output with response_format

```python
from langchain.agents import create_agent
from pydantic import BaseModel, Field

class ResearchReport(BaseModel):
    title: str = Field(description="Report title")
    summary: str = Field(description="Executive summary")
    key_findings: list[str] = Field(description="List of key findings")
    confidence: float = Field(description="Confidence score 0-1")

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=[search_tool],
    response_format=ResearchReport,    # agent returns structured output
    system_prompt="Research the given topic and produce a structured report.",
)

result = agent.invoke({"messages": [{"role": "user", "content": "Research LangGraph"}]})
# result is typed as ResearchReport
```

### Composing multiple middleware

```python
from langchain.agents import create_agent
from langchain.agents.middleware import (
    HumanInTheLoopMiddleware,
    PIIMiddleware,
    ModelFallbackMiddleware,
    ModelCallLimitMiddleware,
    SummarizationMiddleware,
)

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=[...],
    middleware=[
        PIIMiddleware("email", strategy="redact"),          # outermost
        ModelCallLimitMiddleware(run_limit=20),
        ModelFallbackMiddleware("openai:gpt-4o-mini"),
        SummarizationMiddleware("openai:gpt-4o-mini", trigger=("fraction", 0.8)),
        HumanInTheLoopMiddleware({"delete_file": True}),    # innermost
    ],
    checkpointer=...,   # needed for HITL + persistence
)
```

## API Reference

### `create_agent()` parameters

| Param | Type | Description |
|-------|------|-------------|
| `model` | `str \| BaseChatModel` | Model string (`"provider:model"`) or instance |
| `tools` | `list` | Tools, callables, or dicts |
| `system_prompt` | `str \| SystemMessage` | System prompt |
| `middleware` | `list[AgentMiddleware]` | Middleware stack (first = outermost) |
| `response_format` | `type[BaseModel] \| dict` | Structured output schema |
| `checkpointer` | `Checkpointer` | State persistence (required for HITL) |
| `store` | `BaseStore` | Cross-thread storage |
| `interrupt_before` | `list[str]` | Node names to interrupt before |
| `interrupt_after` | `list[str]` | Node names to interrupt after |
| `name` | `str` | Name for subgraph use |
| `debug` | `bool` | Enable verbose logging |

### Model string format

```python
"provider:model-name"   # e.g.:
"anthropic:claude-sonnet-4-6"
"openai:gpt-4o"
"openai:gpt-4o-mini"
"google:gemini-2.0-flash"
"ollama:llama3.1"
```

### Middleware catalog

| Middleware | Constructor | Key params |
|------------|-------------|------------|
| `HumanInTheLoopMiddleware` | `(interrupt_on)` | Per-tool approve/edit/reject/respond |
| `PIIMiddleware` | `(pii_type, strategy)` | block/redact/mask/hash |
| `ModelFallbackMiddleware` | `(model1, model2, ...)` | Failover chain |
| `ModelCallLimitMiddleware` | `(thread_limit, run_limit)` | Call counting |
| `ToolCallLimitMiddleware` | `(thread_limit, run_limit)` | Tool call counting |
| `ModelRetryMiddleware` | `(max_retries, ...)` | Retry failed model calls |
| `ToolRetryMiddleware` | `(max_retries, ...)` | Retry failed tool calls |
| `SummarizationMiddleware` | `(model, trigger, keep)` | Auto-compress context |
| `ContextEditingMiddleware` | `(edits)` | Modify message history |
| `ShellToolMiddleware` | — | Shell command execution |
| `TodoListMiddleware` | — | Planning with todo lists |
| `LLMToolSelectorMiddleware` | `(model)` | LLM-based tool selection |
| `FilesystemFileSearchMiddleware` | — | File search tools |

## Reference Files

| File | Size | Contents |
|------|------|----------|
| `references/api.md` | 500 KB | Full API reference |
| `references/llms.md` | 28 KB | Doc index |
| `references/llms-full.md` | 500 KB | Complete page content |

Source: `https://reference.langchain.com/python/langchain`  
Agent docs: `https://docs.langchain.com/oss/python/langchain/agents`  
Middleware docs: `https://docs.langchain.com/oss/python/langchain/middleware`

Related Skills

langchain-redis

11
from enuno/claude-command-and-control

LangChain Redis integration — RedisVectorStore for RAG, RedisCache and RedisSemanticCache for LLM response caching, RedisChatMessageHistory for persistent conversation memory, and RedisConfig for connection management. Requires Redis Stack (redis/redis-stack-server).

langchain-postgres

11
from enuno/claude-command-and-control

LangChain PostgreSQL integration — PGVectorStore (v2, recommended) and PGVector (v1 legacy) for pgvector RAG, PostgresChatMessageHistory for persistent chat, HNSW/IVFFlat index management, hybrid search, async-first engine via PGEngine, and custom metadata columns.

langchain-perplexity

11
from enuno/claude-command-and-control

LangChain Perplexity AI integration — ChatPerplexity (chat model with built-in web search and date/domain filtering), PerplexitySearchRetriever for RAG, PerplexitySearchResults tool, PerplexityEmbeddings, and reasoning output parsers (ReasoningJsonOutputParser, strip_think_tags).

langchain-openrouter

11
from enuno/claude-command-and-control

LangChain OpenRouter integration — ChatOpenRouter gives access to hundreds of models (Claude, GPT-4o, Gemini, Llama, etc.) through a single API key. Supports provider routing preferences, reasoning models, plugins, tool calling, structured output, and request attribution/tracing.

langchain-ollama

11
from enuno/claude-command-and-control

LangChain Ollama integration — run local LLMs with ChatOllama (chat completions, tool calling, structured output, reasoning/thinking mode), OllamaLLM (raw text completions), and OllamaEmbeddings. Connects to a local Ollama server at localhost:11434.

langchain-neo4j

11
from enuno/claude-command-and-control

LangChain Neo4j integration — Neo4jGraph for Cypher queries and schema inspection, GraphCypherQAChain for natural-language-to-Cypher Q&A, Neo4jVector for vector/hybrid RAG, Neo4jSaver LangGraph checkpointer, Neo4jChatMessageHistory, and GraphDocument/Node/Relationship for knowledge graph construction.

langchain-mcp-adapters

11
from enuno/claude-command-and-control

LangChain MCP Adapters — connect LangChain agents to MCP (Model Context Protocol) servers. Load MCP tools, prompts, and resources as LangChain-compatible objects. Supports stdio, SSE, StreamableHTTP, and WebSocket transports. Includes interceptors, callbacks, and multi-server management.

langchain-exa

11
from enuno/claude-command-and-control

LangChain Exa integration — semantic web search with ExaSearchRetriever (RAG), ExaSearchResults (agent tool), and ExaFindSimilarResults (find similar URLs). Unique features: use_autoprompt (LLM query rewriting), highlights (excerpts), summary (per-result LLM summaries), livecrawl (real-time), and date filtering.

langchain-deepagents

11
from enuno/claude-command-and-control

LangChain Deep Agents (Python) — build, deploy, and customize stateful long-running agents with virtual filesystems, subagents, human-in-the-loop, and LangSmith observability. Also covers LangGraph, LangChain OSS chains/retrievers, and Agent Server API.

langchain-aws

11
from enuno/claude-command-and-control

LangChain AWS integration — ChatBedrockConverse (Claude/Nova/Llama/Mistral on Bedrock), BedrockEmbeddings, AmazonKnowledgeBasesRetriever, BedrockAgentsRunnable, BedrockRerank, BedrockPromptCachingMiddleware, CodeInterpreterToolkit, BrowserToolkit (computer use), Neptune graph chains, and SageMaker endpoint.

langchainjs

11
from enuno/claude-command-and-control

LangChain.js - TypeScript framework for building LLM-powered applications with agents, chains, RAG, tools, memory, and integrations for OpenAI, Anthropic, Google, and hundreds of other providers

web-artifacts-builder

11
from enuno/claude-command-and-control

Suite of tools for creating elaborate, multi-component claude.ai HTML artifacts using modern frontend web technologies (React, Tailwind CSS, shadcn/ui). Use for complex artifacts requiring state management, routing, or shadcn/ui components - not for simple single-file HTML/JSX artifacts.