Skip to main content

LangChain chat model wrapper for the Claude Code Agent SDK — Mimir distribution of langchain-claude-code with bundled adapter fixes (upstream PRs #2/#4/#6, unmerged). Import package remains ``langchain_claude_code``.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_claude_code_mimir-0.1.1.tar.gz (193.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_claude_code_mimir-0.1.1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file langchain_claude_code_mimir-0.1.1.tar.gz.

File metadata

File hashes

Hashes for langchain_claude_code_mimir-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e74263b6f69c88025dc4f317260c29eee6109c9d1c0e26335d33dc8935ede4bb
MD5 34cf0af061a5dab0f4b55cd7fbe51bc6
BLAKE2b-256 f01dc6ca2c3bda4850b4f44c6a109668a90ce0c04b554c378a224f2228bed0b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_claude_code_mimir-0.1.1.tar.gz:

Publisher: publish.yml on jasoncarreira/langchain-claude-code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_claude_code_mimir-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_claude_code_mimir-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 08d1a3380a78af5eab3a2e16bc31074701c5ce3aaf773d6b2ecfd2af170ac217
MD5 41753c9889e4429de9f6e43cb1e73452
BLAKE2b-256 f3f6be8f79656c89be28ed561d819f47006c6da62670d4932876957a09a9cf24

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_claude_code_mimir-0.1.1-py3-none-any.whl:

Publisher: publish.yml on jasoncarreira/langchain-claude-code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page