LangChain chat model wrapper for the Claude Code Agent SDK. Use Claude Code as a drop-in LangChain BaseChatModel with full tool support, streaming, and LangGraph compatibility.
Claude Code CLI (required):
npm install -g @anthropic-ai/claude-codepip install langchain-claude-codeWith example dependencies:
pip install langchain-claude-code[examples]from langchain_claude_code import ClaudeCodeChatModel
model = ClaudeCodeChatModel(api_key="sk-ant-...")Or set the environment variable:
export ANTHROPIC_API_KEY="sk-ant-..."from langchain_claude_code import ClaudeCodeChatModel
model = ClaudeCodeChatModel(oauth_token="...")Or set the environment variable:
export CLAUDE_CODE_OAUTH_TOKEN="..."import asyncio
from langchain_claude_code import ClaudeCodeChatModel
from langchain_core.messages import HumanMessage
async def main():
model = ClaudeCodeChatModel(model="sonnet")
response = await model.ainvoke([HumanMessage(content="What is 2 + 2?")])
print(response.content)
asyncio.run(main())from langchain_claude_code import ClaudeCodeChatModel
from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchTool
model = ClaudeCodeChatModel(model="haiku")
model = model.bind_tools([DuckDuckGoSearchTool()])
response = await model.ainvoke([
HumanMessage(content="Search for the latest news about AI")
])from langchain_claude_code import ClaudeCodeChatModel, ClaudeTool
model = ClaudeCodeChatModel(
model="sonnet",
allowed_tools=[
ClaudeTool.WEB_SEARCH,
ClaudeTool.WEB_FETCH,
ClaudeTool.BASH,
ClaudeTool.READ,
ClaudeTool.WRITE,
],
)async for chunk in model.astream([HumanMessage(content="Write a poem")]):
print(chunk.content, end="", flush=True)| Parameter | Type | Default | Description |
|---|---|---|---|
model |
str |
"opus" |
Model to use: opus, sonnet, haiku |
permission_mode |
str |
"default" |
Permission mode: default, acceptEdits, plan, bypassPermissions |
allowed_tools |
list |
[] |
List of allowed Claude Code tools |
disallowed_tools |
list |
[] |
List of disallowed tools |
system_prompt |
str |
None |
Custom system prompt |
max_turns |
int |
None |
Maximum conversation turns |
max_budget_usd |
float |
None |
Maximum budget in USD |
cwd |
str |
None |
Working directory for file operations |
api_key |
str |
None |
Anthropic API key |
oauth_token |
str |
None |
Claude Code OAuth token |
| Mode | Description |
|---|---|
default |
Prompts for confirmation on potentially dangerous operations |
acceptEdits |
Automatically accepts file edits without confirmation |
plan |
Planning mode - generates plans without executing |
bypassPermissions |
Bypasses all permission checks (use with caution) |
Security Note: When using
acceptEditsorbypassPermissions, Claude Code will modify your filesystem without confirmation. Use these modes only in sandboxed environments or when you trust the input.
from langchain_claude_code import ClaudeTool
# File operations
ClaudeTool.READ
ClaudeTool.WRITE
ClaudeTool.EDIT
ClaudeTool.GLOB
ClaudeTool.GREP
# Shell
ClaudeTool.BASH
ClaudeTool.BASH_OUTPUT
# Web
ClaudeTool.WEB_SEARCH
ClaudeTool.WEB_FETCH
# Other
ClaudeTool.TASK
ClaudeTool.TODO_WRITESee the examples/ directory for complete examples:
bind_tools_example.py- Using LangChain tools with Claude Codedeepagents_example.py- Integration with DeepAgents/LangGraph
MIT License - see LICENSE for details.