Skip to main content

langchain-claude-code

PyPI version License: MIT Python 3.11+

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.

Prerequisites

Claude Code CLI (required):

npm install -g @anthropic-ai/claude-code

Installation

pip install langchain-claude-code

With example dependencies:

pip install langchain-claude-code[examples]

Authentication

Option 1: API Key (for Anthropic API users)

from langchain_claude_code import ClaudeCodeChatModel

model = ClaudeCodeChatModel(api_key="sk-ant-...")

Or set the environment variable:

export ANTHROPIC_API_KEY="sk-ant-..."

Option 2: OAuth Token (for Claude Max subscribers)

from langchain_claude_code import ClaudeCodeChatModel

model = ClaudeCodeChatModel(oauth_token="...")

Or set the environment variable:

export CLAUDE_CODE_OAUTH_TOKEN="..."

Quick Start

Basic Usage

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())

With Tool Binding

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")
])

Using Claude Code's Built-in Tools

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,
    ],
)

Streaming

async for chunk in model.astream([HumanMessage(content="Write a poem")]):
    print(chunk.content, end="", flush=True)

Configuration

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

Permission Modes

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 acceptEdits or bypassPermissions, Claude Code will modify your filesystem without confirmation. Use these modes only in sandboxed environments or when you trust the input.

Available Tools

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_WRITE

Examples

See the examples/ directory for complete examples:

  • bind_tools_example.py - Using LangChain tools with Claude Code
  • deepagents_example.py - Integration with DeepAgents/LangGraph

License

MIT License - see LICENSE for details.

Metadata

Release files for langchain-claude-code 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-claude-code 0.1.0
File Size Uploaded Provenance
langchain_claude_code-0.1.0.tar.gz 184.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-claude-code 0.1.0
File Interpreter ABI Platform
langchain_claude_code-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 195.7 kB

Release files / langchain_claude_code-0.1.0.tar.gz

Download URL langchain_claude_code-0.1.0.tar.gz
Size 184.9 kB
Tags Source
SHA-256 checksum
How to use checksums
959273ee091374e087a657846a0258c287e0dbd7c4c0c8361875bc53e6babadb
BLAKE2b-256 checksum
How to use checksums
e56b0d4ecd906dd70a8f018cf555360e71718e48e4c8856994c9a53cac423297
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / langchain_claude_code-0.1.0-py3-none-any.whl

Download URL langchain_claude_code-0.1.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f98d851654793e1d8cf3cc42aa5a33ff32b46e5f491ba8ff51d08b3279b507a
BLAKE2b-256 checksum
How to use checksums
b7bdaef448681b6ebc1bca59fd9a67019f5225caec1ea17a1809930eec5491a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Security for langchain-claude-code 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page