AI agents have moved from science fiction to production-ready tools. With Anthropic's Claude as the brain and LangChain as the orchestration framework, any developer can build agents that reason, use external tools, and solve complex tasks autonomously. In this practical guide, you will understand the architecture behind these agents, see working code, and walk away ready to build your own.

I have been working with AI agents for over a year, and the combination of Claude + LangChain completely changed how I automate tasks. Before, I built rigid pipelines with sequential scripts. Once I started using agents with dynamic reasoning, I realized the hardest part is not the code — it is designing the tools the agent will use. Claude, in particular, has an ability to follow long, complex instructions that surpasses what I achieved with other models. The difference becomes clear when the agent needs to decide between multiple tools in sequence without losing context.

What are AI agents and why they matter

An AI agent is a system that receives a goal, reasons about how to achieve it, and executes actions using available tools — all without human intervention at each step. Unlike a simple chatbot that merely answers questions, an agent makes dynamic decisions: it can fetch data from an API, process a file, run code, and combine results before delivering the final response.

The concept gained practical traction in 2025 when frameworks like LangChain made accessible what previously required heavy engineering. In 2026, according to LangChain's State of Agent Engineering report, 57% of surveyed organizations have deployed agents in production, with another 30% actively developing them. This is no longer an experiment — it is infrastructure.

The basic architecture of an agent has three layers: the language model (LLM) as the brain, the tools as the hands, and memory as persistent context. Claude excels in the first layer thanks to its extensive context window and ability to follow complex instructions without drifting from the objective.

Why Claude as the LLM for agents

Not every model works well as an agent's engine. The model needs to reliably follow instructions, produce consistent output formats, and reason about when and how to use tools. Claude, especially the Opus and Sonnet versions, excels in three aspects critical for agents:

  • Massive context window — with up to 1 million tokens in Opus 4, the agent can maintain long conversations with complete tool-use history without losing information.
  • Instruction adherence — Claude follows long, detailed system prompts with high fidelity, which is essential when you define complex tool-use rules.
  • Native tool use — Claude's API supports function calling natively, eliminating the need for text-parsing hacks.

Additionally, Anthropic launched the Claude Agent SDK in 2026, providing ready-made primitives for building agents with subagents, sessions, and MCP (Model Context Protocol) support. For those wanting maximum integration with the Claude ecosystem, the SDK is the direct path. But for multi-model flexibility and LangSmith observability, LangChain remains the more versatile choice.

LangChain and LangGraph: the framework evolution

LangChain started as a library for chaining LLM calls with tools. The original agent component, AgentExecutor, connected an LLM to a toolset and managed the reasoning-action loop. It worked, but was hard to customize for complex flows.

In 2025, the LangChain team launched LangGraph, which replaced the linear executor with a graph-based architecture. Each graph node represents an agent or processing step, with fine-grained control over flow, retries, and error handling. As of 2026, LangGraph is the recommended standard for production agents.

FeatureAgentExecutor (legacy)LangGraph (current)
ArchitectureLinear loopState graph
Flow controlLimitedConditional, parallel, cyclic
SubagentsNot nativeNative with independent nodes
CheckpointingManualBuilt-in (Postgres, SQLite)
Human-in-the-loopHackFirst-class primitive
StreamingPartialToken-by-token + tool events

Migrating from AgentExecutor to LangGraph is not mandatory for simple cases, but any agent that needs coordinated multi-tool usage, intelligent retries, or conditional human intervention will benefit from the graph model.

Setting up the development environment

Before writing your first agent, you need to set up the environment. Here is the minimum functional setup:

pip install langchain-anthropic langchain-core langgraph python-dotenv

Create a .env file in the project root with your Anthropic API key:

ANTHROPIC_API_KEY=sk-ant-your-key-here

The langchain-anthropic library (version 1.4+ as of June 2026) is the official connector between LangChain and the Claude API. It implements LangChain's BaseChatModel interface, meaning Claude works as a drop-in replacement for any other model in the framework.

To verify everything works, a quick test:

from langchain_anthropic import ChatAnthropic
from dotenv import load_dotenv

load_dotenv()
model = ChatAnthropic(model="claude-sonnet-4-6")
response = model.invoke("Say hello in one sentence.")
print(response.content)

If this returns a response, the environment is ready.

Building your first agent with tools

An agent's power lies in the tools it can use. Let us create a simple agent with access to two tools: a calculator and a weather fetcher. The agent decides on its own which to use based on the user's question.

from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent

@tool
def calculator(expression: str) -> str:
    'Calculates a mathematical expression. Use for any computation.'
    try:
        result = eval(expression)  # In production, use a safe parser
        return f"Result: {result}"
    except Exception as e:
        return f"Error: {e}"

@tool
def current_weather(city: str) -> str:
    'Returns the current weather for a city.'
    # Simulation — in production, connect to a real API
    weather = {"New York": "72°F, cloudy", "San Francisco": "65°F, sunny"}
    return weather.get(city, f"Data not available for {city}")

model = ChatAnthropic(model="claude-sonnet-4-6")
agent = create_react_agent(model, [calculator, current_weather])

# Run the agent
result = agent.invoke({
    "messages": [{"role": "user", "content": "What is the weather in New York? And what is 15% of 3500?"}]
})

for msg in result["messages"]:
    print(f"{msg.type}: {msg.content}")

What happens under the hood: Claude receives the question, identifies that it needs two different tools, calls current_weather with "New York" and calculator with "3500 * 0.15", receives the results, and composes a natural response combining both. All in a single reasoning flow.

Anatomy of the ReAct loop

The ReAct (Reasoning + Acting) pattern is the heart of most agents. The cycle works like this: the LLM receives the context, reasons about the next step, decides if it needs a tool, executes the action, observes the result, and repeats until it has enough information to respond. The create_react_agent from LangGraph implements this loop as a graph with two nodes — the model node and the tool execution node — connected in a cycle.

Advanced agents: memory, subagents, and MCP

A basic agent with tools already solves many problems. But for production scenarios, you need three additional capabilities: persistent memory, delegation to subagents, and integration with external services via MCP.

Persistent memory with checkpointing

LangGraph supports native checkpointing — at each agent step, the state is saved to a backend (SQLite for development, Postgres for production). This allows the agent to resume a conversation exactly where it left off, even after restarting the process:

from langgraph.checkpoint.sqlite import SqliteSaver

memory = SqliteSaver.from_conn_string(":memory:")
agent = create_react_agent(model, tools, checkpointer=memory)

# First conversation
config = {"configurable": {"thread_id": "user-123"}}
agent.invoke({"messages": [{"role": "user", "content": "My name is Ana"}]}, config)

# Second conversation — the agent remembers
agent.invoke({"messages": [{"role": "user", "content": "What is my name?"}]}, config)
# Response: "Your name is Ana"

Subagents for complex tasks

When a task is too large for a single agent, you can decompose it into specialized subagents. In LangGraph, each subagent is a graph node that can have its own tools and instructions. An orchestrator agent decides which subagent to invoke based on the task.

For example, a code analysis agent might have three subagents: one for finding relevant files, another for analyzing dependencies, and a third for suggesting refactorings. Claude Opus, with its 1M token context, is particularly effective as an orchestrator because it can maintain the complete state of all subagents simultaneously.

Model Context Protocol (MCP)

The MCP is an open protocol that standardizes how agents connect to data sources and external services. Instead of creating custom integrations for each API, you expose your services as MCP servers and the agent connects automatically. LangGraph has supported MCP natively since version 1.2, and the Claude Agent SDK was built with MCP as a central primitive.

In practice, this means an agent can access databases, file systems, internal APIs, and third-party tools through a unified interface, without the developer needing to write specific integration code for each one.

Best practices for production agents

Putting an agent in production requires care beyond functional code. Here are the practices I have learned through experience:

  • Limit the number of iterations — set a max_iterations to prevent infinite loops. An agent that cannot solve a task in 10 steps probably needs better tools, not more attempts.
  • Monitor with LangSmith — LangChain's observability platform lets you trace every agent step, including tokens consumed, latency of each tool call, and the complete reasoning chain. Essential for production debugging.
  • Validate tool outputs — tools can return errors or unexpected data. Add validation to returns before feeding them back to the agent.
  • Use streaming for UX — agents take time. Token-by-token streaming keeps users engaged while the agent works. LangGraph supports streaming tool events beyond just text.
  • Implement circuit breakers — if an external tool is down, the agent should not keep retrying indefinitely. Configure timeouts and fallbacks.
  • Test with adversarial scenarios — agents are non-deterministic. Create tests that cover ambiguous inputs, failing tools, and requests the agent should not fulfill.

Claude Agent SDK vs LangChain: when to use each

The choice between the Claude Agent SDK and LangChain is not binary — it depends on your use case. According to a comparative analysis published in March 2026, the decision comes down to two questions: do you need multi-model flexibility? And how much control do you want over orchestration?

CriterionClaude Agent SDKLangChain / LangGraph
ModelClaude onlyAny LLM
SetupMinimal — batteries includedRequires configuration
OrchestrationManaged internal loopCustomizable graph
ObservabilityBasicFull LangSmith
MCPNative, centralSupported via integration
SubagentsYes, with sessionsYes, as graph nodes
Hosted executionYes (billing via Anthropic)Self-hosted or LangServe

If your stack is 100% Claude and you want to move fast, the Agent SDK significantly reduces boilerplate. If you need to swap models, want detailed observability, or need to create complex orchestration flows with conditional graphs, LangChain with LangGraph is more suitable. Many teams use both: the Agent SDK for simple internal agents and LangGraph for complex production pipelines.

Conclusion

Building AI agents with Claude and LangChain is no longer a technology bet — it is an approach validated by thousands of companies in production. Claude provides the reasoning capability and instruction adherence that a reliable agent demands, while LangChain and LangGraph offer the orchestration structure to turn that reasoning into coordinated actions. The fastest path to get started is the ReAct example in this article: define two or three real tools from your domain, connect to Claude via langchain-anthropic, and let the agent solve it. You will be surprised by what a well-configured reasoning loop can do without you having to code every decision.