Claude API Integration with LangChain: Setup Guide
LangChain is one of the most common frameworks developers reach for when building LLM-powered applications, and Claude is one of the strongest models to plug into it. Integrating the Claude API with LangChain means installing the langchain-anthropic package, setting your API key, and instantiating a ChatAnthropic object that slots into any LangChain chain, agent, or retrieval pipeline you already have.
This guide walks through the full setup: installing dependencies, authenticating, running basic chat completions, streaming responses, using tools, and handling memory — all with Claude as the underlying model. If you're evaluating whether to go through LangChain directly against Anthropic's API or through a gateway like SubToAPI, we'll cover that too.
Installing the Required Packages
LangChain splits provider integrations into separate packages. For Claude, you need:
pip install langchain langchain-anthropic
If you're working in JavaScript/TypeScript, the equivalent is:
npm install langchain @langchain/anthropic
Both packages wrap the Anthropic Messages API and expose it through LangChain's standard ChatModel interface, so anything you've built against OpenAI or other providers in LangChain will work with minimal changes.
Setting Up Authentication
LangChain's ChatAnthropic class reads the API key from an environment variable by default:
export ANTHROPIC_API_KEY="your-key-here"
Or you can pass it explicitly in code:
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model="claude-3-5-sonnet-20241022",
anthropic_api_key="your-key-here",
temperature=0.3,
)
If you're routing requests through a proxy or gateway instead of calling Anthropic directly, you typically override the anthropic_api_url parameter (or the equivalent base URL config) to point at your gateway's endpoint, while keeping the same ChatAnthropic interface.
Basic Chat Completion
Once the model object is instantiated, calling Claude is identical to calling any other LangChain chat model:
from langchain_core.messages import HumanMessage, SystemMessage
messages = [
SystemMessage(content="You are a concise technical writer."),
HumanMessage(content="Explain what a vector database is in two sentences."),
]
response = llm.invoke(messages)
print(response.content)
This returns a standard LangChain AIMessage, so it works seamlessly with chains, output parsers, and retrievers you've already built.
Streaming Responses
Claude supports streaming natively, and LangChain exposes it through the same .stream() method used for other providers:
for chunk in llm.stream(messages):
print(chunk.content, end="", flush=True)
This is useful for chat UIs where you want tokens to appear incrementally rather than waiting for the full completion.
Using Claude's Tool Calling in LangChain
Claude supports tool use (function calling), and LangChain's bind_tools() method maps directly onto it:
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"It's 18°C and cloudy in {city}."
llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in Lisbon?")
print(response.tool_calls)
LangChain handles the schema conversion from your Python function signature to the JSON schema Claude expects, and parses Claude's tool-call response back into a usable Python object.
Building a Simple Chain with Memory
A common pattern is combining Claude with conversation memory so multi-turn context is preserved:
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
memory = ConversationBufferMemory()
conversation = ConversationChain(llm=llm, memory=memory)
conversation.predict(input="My name is Alex.")
conversation.predict(input="What's my name?")
LangChain manages message history and injects it into each call to Claude automatically, so you don't have to manually track the conversation array yourself.
Retrieval-Augmented Generation (RAG) with Claude
Claude works the same way as any other chat model inside a LangChain RAG pipeline:
from langchain.chains import RetrievalQA
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=my_vectorstore.as_retriever(),
)
result = qa_chain.invoke({"query": "What does our refund policy say about digital goods?"})
print(result["result"])
Nothing Claude-specific changes here — the retriever handles document lookup, and Claude handles synthesis.
When Direct Integration Gets Complicated
LangChain abstracts a lot, but a few things still land on you: managing separate API keys per environment or team member, tracking usage and cost per request, handling retries and rate limits, and giving non-engineering teammates visibility into what's being spent.
If you're already issuing Claude access through a Claude.ai or Anthropic Console seat and want to turn that into a proper HTTPS API with per-application keys, usage metadata, and team-level billing, SubToAPI sits underneath your LangChain setup without changing the integration pattern. You point ChatAnthropic at SubToAPI's endpoint instead of Anthropic's directly, authenticate with a sub_live_... key, and everything else — streaming, tool use, message formatting — works the same way, documented at /docs/messages and /docs/streaming.
This is particularly useful for teams where multiple developers or services need Claude access but you want a single dashboard for keys and usage instead of sharing one raw Anthropic key across the org. Plans start at €9/month on the Solo tier, with Team (€19/seat) and Scale (€49/seat) options for larger setups, and a free trial at /signup.
Quick Checklist for Production
- Pin your model version string (e.g.
claude-3-5-sonnet-20241022) rather than using a floating alias, so behavior doesn't shift unexpectedly. - Set explicit
max_tokensandtemperature— LangChain's defaults aren't always what you want for production. - Wrap
.invoke()calls in your own retry logic for rate limits or transient errors; LangChain doesn't retry by default. - If multiple services call Claude through LangChain, issue separate keys per service so you can track usage and revoke access independently. See /docs/quickstart for key setup patterns.
questions
Does LangChain support all Claude features, including tool use and streaming? Yes. langchain-anthropic supports Claude's tool calling, streaming, and system prompts through the standard LangChain chat model interface, so you don't need to drop down to raw API calls for common use cases.
Can I use LangChain with Claude without an Anthropic API key, using a shared team subscription instead? Not directly through Anthropic, but you can route ChatAnthropic through a gateway like SubToAPI that turns a Claude subscription into standard API keys, letting you keep the same LangChain code while managing access centrally.
Which Claude model should I use with LangChain for production apps? Claude 3.5 Sonnet is the common default for a balance of quality and cost. Use Haiku for high-volume, latency-sensitive tasks, and pin the exact model version string rather than relying on a floating alias.