Get started using Anthropic chat models in LangChain.
You can find information about Anthropic’s latest models, their costs, context windows, and supported input types in the Claude docs.
API ReferenceFor detailed documentation of all features and configuration options, head to the ChatAnthropic API reference.
AWS Bedrock and Google VertexAINote that certain Anthropic models can also be accessed via AWS Bedrock and Google VertexAI. See the ChatBedrock and ChatVertexAI integrations to use Anthropic models via these services.
messages = [ ( "system", "You are a helpful assistant that translates English to French. Translate the user sentence.", ), ("human", "I love programming."),]ai_msg = model.invoke(messages)ai_msg
from langchain_anthropic import ChatAnthropicfrom langchain.messages import HumanMessage, SystemMessagemodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")messages = [ SystemMessage(content="You are a scientist"), HumanMessage(content="Hello, Claude"),]token_count = model.get_num_tokens_from_messages(messages)print(token_count)
Copy
14
You can also count tokens when using tools:
Copy
from langchain.tools import tool@tool(parse_docstring=True)def get_weather(location: str) -> str: """Get the current weather in a given location Args: location: The city and state, e.g. San Francisco, CA """ return "Sunny"messages = [ HumanMessage(content="What's the weather like in San Francisco?"),]token_count = model.get_num_tokens_from_messages(messages, tools=[get_weather])print(token_count)
When using tools, extended thinking, and other features, content from a single Anthropic AIMessage can either be a single string or a list of Anthropic content blocks.For example, when an Anthropic model invokes a tool, the tool invocation is part of the message content (as well as being exposed in the standardized AIMessage.tool_calls):
Copy
from langchain_anthropic import ChatAnthropicfrom typing_extensions import Annotatedmodel = ChatAnthropic(model="claude-haiku-4-5-20251001")def get_weather( location: Annotated[str, ..., "Location as city and state."]) -> str: """Get the weather at a location.""" return "It's sunny."model_with_tools = model.bind_tools([get_weather])response = model_with_tools.invoke("Which city is hotter today: LA or NY?")response.content
Copy
[{'text': "I'll help you compare the temperatures of Los Angeles and New York by checking their current weather. I'll retrieve the weather for both cities.", 'type': 'text'}, {'id': 'toolu_01CkMaXrgmsNjTso7so94RJq', 'input': {'location': 'Los Angeles, CA'}, 'name': 'get_weather', 'type': 'tool_use'}, {'id': 'toolu_01SKaTBk9wHjsBTw5mrPVSQf', 'input': {'location': 'New York, NY'}, 'name': 'get_weather', 'type': 'tool_use'}]
Using content_blocks will render the content in LangChain’s standard format that is consistent across other model providers. Read more about content blocks.
Copy
response.content_blocks
You can also access tool calls specifically in a standard format using the
tool_calls attribute:
Anthropic’s tool use features allow you to define external functions that Claude can call during a conversation. This enables dynamic information retrieval, computations, and interactions with external systems.See ChatAnthropic.bind_tools for details on how to bind tools to your model instance.
For information about Claude’s built-in tools (code execution, web browsing, files API, etc), see the Built-in tools.
Anthropic supports opt-in strict schema adherence to tool calls. This guarantees that tool names and arguments are validated and correctly typed through constrained decoding.Without strict mode, Claude can occasionally generate invalid tool inputs that break your applications:
Type mismatches: passengers: "2" instead of passengers: 2
Missing required fields: Omitting fields your function expects
Invalid enum values: Values outside the allowed set
Schema violations: Nested objects not matching expected structure
Strict tool use guarantees schema-compliant tool calls:
Tool inputs strictly follow your input_schema
Guaranteed field types and required fields
Eliminate error handling for malformed inputs
Tool name used is always from provided tools
Use strict tool use
Use standard tool calling
Building agentic workflows where reliability is critical
Simple, single-turn tool calls
Tools with many parameters or nested objects
Prototyping and experimentation
Functions that require specific types (e.g., int vs str)
To enable strict tool use, specify strict=True when calling bind_tools.
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")def get_weather(location: str) -> str: """Get the weather at a location.""" return "It's sunny."model_with_tools = model.bind_tools([get_weather], strict=True)
Example: Type-safe booking system
Consider a booking system where passengers must be an integer:
Copy
from langchain_anthropic import ChatAnthropicfrom typing import Literalmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")def book_flight( destination: str, departure_date: str, passengers: int, cabin_class: Literal["economy", "business", "first"]) -> str: """Book a flight to a destination. Args: destination: The destination city departure_date: Date in YYYY-MM-DD format passengers: Number of passengers (must be an integer) cabin_class: The cabin class for the flight """ return f"Booked {passengers} passengers to {destination}"model_with_tools = model.bind_tools( [book_flight], strict=True, tool_choice="any",)response = model_with_tools.invoke("Book 2 passengers to Tokyo, business class, 2025-01-15")# With strict=True, passengers is guaranteed to be int, not "2" or "two"print(response.tool_calls[0]["args"]["passengers"])
Copy
2
Strict tool use has some JSON schema limitations to be aware of. See the Claude docs for more details.If your tool schema uses unsupported features, you’ll receive a 400 error. In these cases, simplify the schema or use standard (non-strict) tool calling.
For complex tools, you can provide usage examples to help Claude understand how to use them correctly. This is done by setting input_examples in the tool’s extras parameter.
Copy
from langchain_anthropic import ChatAnthropicfrom langchain.tools import tool@tool( extras={ "input_examples": [ { "query": "weather report", "location": "San Francisco", "format": "detailed" }, { "query": "temperature", "location": "New York", "format": "brief" } ] } )def search_weather_data(query: str, location: str, format: str = "brief") -> str: """Search weather database with specific query and format preferences. Args: query: The type of weather information to retrieve location: City or region to search format: Output format, either 'brief' or 'detailed' """ return f"{format.title()} {query} for {location}: Data found"model = ChatAnthropic(model="claude-sonnet-4-5-20250929")model_with_tools = model.bind_tools([search_weather_data])response = model_with_tools.invoke( "Get me a detailed weather report for Seattle")
The extras parameter also supports:
defer_loading (bool): Load tool on-demand for tool search
cache_control (dict): Enable prompt caching for the tool
Anthropic supports fine-grained tool streaming, a beta feature that reduces latency when streaming tool calls with large parameters.Rather than buffering entire parameter values before transmission, fine-grained streaming sends parameter data as it becomes available. This can reduce the initial delay from 15 seconds to around 3 seconds for large tool parameters.
Fine-grained streaming may return invalid or partial JSON inputs, especially if the response reaches max_tokens before completing. Implement appropriate error handling for incomplete JSON data.
To enable fine-grained tool streaming, specify the fine-grained-tool-streaming-2025-05-14 beta header when initializing the model:
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic( model="claude-sonnet-4-5-20250929", betas=["fine-grained-tool-streaming-2025-05-14"], )def write_document(title: str, content: str) -> str: """Write a document with the given title and content.""" return f"Document '{title}' written successfully"model_with_tools = model.bind_tools([write_document])# Stream tool calls with reduced latencyfor chunk in model_with_tools.stream( "Write a detailed technical document about the benefits of streaming APIs"): print(chunk.content)
The streaming data arrives as input_json_delta blocks in chunk.content. You can accumulate these to build the complete tool arguments:
Copy
import jsonaccumulated_json = ""for chunk in model_with_tools.stream("Write a document about AI"): for block in chunk.content: if isinstance(block, dict) and block.get("type") == "input_json_delta": accumulated_json += block.get("partial_json", "") try: # Try to parse accumulated JSON parsed = json.loads(accumulated_json) print(f"Complete args: {parsed}") except json.JSONDecodeError: # JSON is still incomplete, continue accumulating pass
Copy
Complete args: {'title': 'Artificial Intelligence: An Overview', 'content': '# Artificial Intelligence: An Overview...
Claude supports image and PDF inputs as content blocks, both in Anthropic’s native format (see docs for vision and PDF support) as well as LangChain’s standard format.
The Files API can also be used to upload files to a container for use with Claude’s built-in code-execution tools. See the code execution section for details.
Some Claude models support an extended thinking feature, which will output the step-by-step reasoning process that led to its final answer.See compatible models in the Claude documentation.To use extended thinking, specify the thinking parameter when initializing ChatAnthropic. If needed, it can also be passed in as a parameter during invocation.You will need to specify a token budget to use this feature. See usage example below:
Copy
import jsonfrom langchain_anthropic import ChatAnthropicmodel = ChatAnthropic( model="claude-sonnet-4-5-20250929", max_tokens=5000, thinking={"type": "enabled", "budget_tokens": 2000}, )response = model.invoke("What is the cube root of 50.653?")print(json.dumps(response.content_blocks, indent=2))
Copy
[ { "type": "reasoning", "reasoning": "To find the cube root of 50.653, I need to find the value of $x$ such that $x^3 = 50.653$.\n\nI can try to estimate this first. \n$3^3 = 27$\n$4^3 = 64$\n\nSo the cube root of 50.653 will be somewhere between 3 and 4, but closer to 4.\n\nLet me try to compute this more precisely. I can use the cube root function:\n\ncube root of 50.653 = 50.653^(1/3)\n\nLet me calculate this:\n50.653^(1/3) \u2248 3.6998\n\nLet me verify:\n3.6998^3 \u2248 50.6533\n\nThat's very close to 50.653, so I'm confident that the cube root of 50.653 is approximately 3.6998.\n\nActually, let me compute this more precisely:\n50.653^(1/3) \u2248 3.69981\n\nLet me verify once more:\n3.69981^3 \u2248 50.652998\n\nThat's extremely close to 50.653, so I'll say that the cube root of 50.653 is approximately 3.69981.", "extras": {"signature": "ErUBCkYIBxgCIkB0UjV..."} }, { "type": "text" "text": "The cube root of 50.653 is approximately 3.6998.\n\nTo verify: 3.6998\u00b3 = 50.6530, which is very close to our original number.", }]
Certain Claude models support an effort feature, which controls how many tokens Claude uses when responding. This is useful for balancing response quality against latency and cost.
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic( model="claude-opus-4-5-20251101", effort="medium", )response = model.invoke("Analyze the trade-offs between microservices and monolithic architectures")
Setting effort to "high" produces exactly the same behavior as omitting the parameter altogether.
See the Claude documentation for detail on when to use different effort levels and to see supported models.
Anthropic supports caching of elements of your prompts, including messages, tool definitions, tool results, images and documents. This allows you to re-use large documents, instructions, few-shot documents, and other data to reduce latency and costs.To enable caching on an element of a prompt, mark its associated content block using the cache_control key. See examples below:
Only certain Claude models support prompt caching. See the Claude documentation for details.
First invocation:{'cache_read': 0, 'cache_creation': 1458}Second:{'cache_read': 1458, 'cache_creation': 0}
Extended cachingThe cache lifetime is 5 minutes by default. If this is too short, you can apply one hour caching by enabling the "extended-cache-ttl-2025-04-11" beta header and specifying "cache_control": {"type": "ephemeral", "ttl": "1h"} on the message:
Incremental caching in conversational applications
Prompt caching can be used in multi-turn conversations to maintain context from earlier messages without redundant processing.We can enable incremental caching by marking the final message with cache_control. Claude will automatically use the longest previously-cached prefix for follow-up messages.Below, we implement a simple chatbot that incorporates this feature. We follow the LangChain chatbot tutorial, but add a custom reducer that automatically marks the last content block in each user message with cache_control:
Chatbot with incremental prompt caching
Copy
import requestsfrom langchain_anthropic import ChatAnthropicfrom langgraph.checkpoint.memory import MemorySaverfrom langgraph.graph import START, StateGraph, add_messagesfrom typing_extensions import Annotated, TypedDictmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")# Pull LangChain readmeget_response = requests.get( "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md")readme = get_response.textdef messages_reducer(left: list, right: list) -> list: # Update last user message for i in range(len(right) - 1, -1, -1): if right[i].type == "human": right[i].content[-1]["cache_control"] = {"type": "ephemeral"} break return add_messages(left, right)class State(TypedDict): messages: Annotated[list, messages_reducer]workflow = StateGraph(state_schema=State)# Define the function that calls the modeldef call_model(state: State): response = model.invoke(state["messages"]) return {"messages": [response]}# Define the (single) node in the graphworkflow.add_edge(START, "model")workflow.add_node("model", call_model)# Add memorymemory = MemorySaver()app = workflow.compile(checkpointer=memory)
================================== Ai Message ==================================Hello, Bob! It's nice to meet you. How are you doing today? Is there something I can help you with?{'cache_read': 0, 'cache_creation': 0, 'ephemeral_5m_input_tokens': 0, 'ephemeral_1h_input_tokens': 0}
Copy
query = f"Check out this readme: {readme}"input_message = HumanMessage([{"type": "text", "text": query}])output = app.invoke({"messages": [input_message]}, config)output["messages"][-1].pretty_print()print(f"\n{output['messages'][-1].usage_metadata['input_token_details']}")
Copy
================================== Ai Message ==================================I can see you've shared the README from the LangChain GitHub repository. This is the documentation for LangChain, which is a popular framework for building applications powered by Large Language Models (LLMs). Here's a summary of what the README contains:LangChain is:- A framework for developing LLM-powered applications- Helps chain together components and integrations to simplify AI application development- Provides a standard interface for models, embeddings, vector stores, etc.Key features/benefits:- Real-time data augmentation (connect LLMs to diverse data sources)- Model interoperability (swap models easily as needed)- Large ecosystem of integrationsThe LangChain ecosystem includes:- LangSmith - For evaluations and observability- LangGraph - For building complex agents with customizable architecture- LangSmith - For deployment and scaling of agentsThe README also mentions installation instructions (`pip install -U langchain`) and links to various resources including tutorials, how-to guides, conceptual guides, and API references.Is there anything specific about LangChain you'd like to know more about, Bob?{'cache_read': 0, 'cache_creation': 1846, 'ephemeral_5m_input_tokens': 1846, 'ephemeral_1h_input_tokens': 0}
Copy
query = "What was my name again?"input_message = HumanMessage([{"type": "text", "text": query}])output = app.invoke({"messages": [input_message]}, config)output["messages"][-1].pretty_print()print(f"\n{output['messages'][-1].usage_metadata['input_token_details']}")
Copy
================================== Ai Message ==================================Your name is Bob. You introduced yourself at the beginning of our conversation.{'cache_read': 1846, 'cache_creation': 278, 'ephemeral_5m_input_tokens': 278, 'ephemeral_1h_input_tokens': 0}
In the LangSmith trace, toggling “raw output” will show exactly what messages are sent to the chat model, including cache_control keys.
Anthropic supports a citations feature that lets Claude attach context to its answers based on source documents supplied by the user.When document or search_result content blocks with "citations": {"enabled": True} are included in a query, Claude may generate citations in its response.
In this example we pass a plain text document. In the background, Claude automatically chunks the input text into sentences, which are used when generating citations.
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic(model="claude-haiku-4-5-20251001")messages = [ { "role": "user", "content": [ { "type": "document", "source": { "type": "text", "media_type": "text/plain", "data": "The grass is green. The sky is blue.", }, "title": "My Document", "context": "This is a trustworthy document.", "citations": {"enabled": True}, }, {"type": "text", "text": "What color is the grass and sky?"}, ], }]response = model.invoke(messages)response.content
Copy
[{'text': 'Based on the document, ', 'type': 'text'}, {'text': 'the grass is green', 'type': 'text', 'citations': [{'type': 'char_location', 'cited_text': 'The grass is green. ', 'document_index': 0, 'document_title': 'My Document', 'start_char_index': 0, 'end_char_index': 20}]}, {'text': ', and ', 'type': 'text'}, {'text': 'the sky is blue', 'type': 'text', 'citations': [{'type': 'char_location', 'cited_text': 'The sky is blue.', 'document_index': 0, 'document_title': 'My Document', 'start_char_index': 20, 'end_char_index': 36}]}, {'text': '.', 'type': 'text'}]
Claude supports a search_result content block representing citable results from queries against a knowledge base or other custom source. These content blocks can be passed to claude both top-line (as in the above example) and within a tool result. This allows Claude to cite elements of its response using the result of a tool call.To pass search results in response to tool calls, define a tool that returns a list of search_result content blocks in Anthropic’s native format. For example:
Copy
def retrieval_tool(query: str) -> list[dict]: """Access my knowledge base.""" # Run a search (e.g., with a LangChain vector store) results = vector_store.similarity_search(query=query, k=2) # Package results into search_result blocks return [ { "type": "search_result", # Customize fields as desired, using document metadata or otherwise "title": "My Document Title", "source": "Source description or provenance", "citations": {"enabled": True}, "content": [{"type": "text", "text": doc.page_content}], } for doc in results ]
End to end example with LangGraph
Here we demonstrate an end-to-end example in which we populate a LangChain vector store with sample documents and equip Claude with a tool that queries those documents.The tool here takes a search query and a category string literal, but any valid tool signature can be used.This example requires langchain-openai and numpy to be installed:
Copy
pip install langchain-openai numpy
Copy
from typing import Literalfrom langchain.chat_models import init_chat_modelfrom langchain.embeddings import init_embeddingsfrom langchain_core.documents import Documentfrom langchain_core.vectorstores import InMemoryVectorStorefrom langgraph.checkpoint.memory import InMemorySaverfrom langchain.agents import create_agent# Set up vector store# Ensure you set your OPENAI_API_KEY environment variableembeddings = init_embeddings("openai:text-embedding-3-small")vector_store = InMemoryVectorStore(embeddings)document_1 = Document( id="1", page_content=( "To request vacation days, submit a leave request form through the " "HR portal. Approval will be sent by email." ), metadata={ "category": "HR Policy", "doc_title": "Leave Policy", "provenance": "Leave Policy - page 1", },)document_2 = Document( id="2", page_content="Managers will review vacation requests within 3 business days.", metadata={ "category": "HR Policy", "doc_title": "Leave Policy", "provenance": "Leave Policy - page 2", },)document_3 = Document( id="3", page_content=( "Employees with over 6 months tenure are eligible for 20 paid vacation days " "per year." ), metadata={ "category": "Benefits Policy", "doc_title": "Benefits Guide 2025", "provenance": "Benefits Policy - page 1", },)documents = [document_1, document_2, document_3]vector_store.add_documents(documents=documents)# Define toolasync def retrieval_tool( query: str, category: Literal["HR Policy", "Benefits Policy"]) -> list[dict]: """Access my knowledge base.""" def _filter_function(doc: Document) -> bool: return doc.metadata.get("category") == category results = vector_store.similarity_search( query=query, k=2, filter=_filter_function ) return [ { "type": "search_result", "title": doc.metadata["doc_title"], "source": doc.metadata["provenance"], "citations": {"enabled": True}, "content": [{"type": "text", "text": doc.page_content}], } for doc in results ]# Create agentmodel = init_chat_model("claude-haiku-4-5-20251001")checkpointer = InMemorySaver()agent = create_agent(model, [retrieval_tool], checkpointer=checkpointer)# Invoke on a queryconfig = {"configurable": {"thread_id": "session_1"}}input_message = { "role": "user", "content": "How do I request vacation days?",}async for step in agent.astream( {"messages": [input_message]}, config, stream_mode="values",): step["messages"][-1].pretty_print()
Anthropic also lets you specify your own splits using custom document types. LangChain text splitters can be used to generate meaningful splits for this purpose. See the below example, where we split the LangChain README.md (a markdown document) and pass it to Claude as context:This example requires langchain-text-splitters to be installed:
Anthropic supports a context editing feature that will automatically manage the model’s context window (e.g., by clearing tool results).See the Claude documentation for more details and configuration options.
Context management is supported since langchain-anthropic>=0.3.21You must speficy the context-management-2025-06-27 beta header to apply context management to your model calls.
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic( model="claude-sonnet-4-5-20250929", betas=["context-management-2025-06-27"], context_management={"edits": [{"type": "clear_tool_uses_20250919"}]}, )model_with_tools = model.bind_tools([{"type": "web_search_20250305", "name": "web_search"}])response = model_with_tools.invoke("Search for recent developments in AI")
Claude Sonnet 4 and 4.5 support a 1-million token context window, available in beta for organizations in usage tier 4 and organizations with custom rate limits.To enable the extended context window, specify the context-1m-2025-08-07 beta header:
Copy
from langchain_anthropic import ChatAnthropicfrom langchain.messages import HumanMessagemodel = ChatAnthropic( model="claude-sonnet-4-5-20250929", betas=["context-1m-2025-08-07"], )long_document = """This is a very long document that would benefit from the extended 1Mcontext window...[imagine this continues for hundreds of thousands of tokens]"""messages = [ HumanMessage(f"""Please analyze this document and provide a summary:{long_document}What are the key themes and main conclusions?""")]response = model.invoke(messages)
Anthropic supports a native structured output feature, which guarantees that its responses adhere to a given schema.You can access this feature in individual model calls, or by specifying the response format of a LangChain agent. See below for examples.
Individual model calls
Use the with_structured_output method to generate a structured model response. Specify method="json_schema" to enable Anthropic’s native structured output feature; otherwise the method defaults to using function calling.
Copy
from langchain_anthropic import ChatAnthropicfrom pydantic import BaseModel, Fieldmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")class Movie(BaseModel): """A movie with details.""" title: str = Field(..., description="The title of the movie") year: int = Field(..., description="The year the movie was released") director: str = Field(..., description="The director of the movie") rating: float = Field(..., description="The movie's rating out of 10")model_with_structure = model.with_structured_output(Movie, method="json_schema") response = model_with_structure.invoke("Provide details about the movie Inception")response
Anthropic supports a variety of built-in tools, which can be bound to the model in the usual way. Claude will generate tool calls adhering to its internal schema for the tool.
Claude supports a bash tool that allows it to execute shell commands in a persistent bash session. This enables system operations, script execution, and command-line automation.
Important: You must provide the execution environmentLangChain handles the API integration (sending/receiving tool calls), but you are responsible for:
Setting up a sandboxed computing environment (Docker, VM, etc.)
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")bash_tool = { "type": "bash_20250124", "name": "bash",}model_with_bash = model.bind_tools([bash_tool])response = model_with_bash.invoke( "List all Python files in the current directory")
response.tool_calls will contain the bash command Claude wants to execute. You must run this command in your environment and pass the result back.
Copy
[{'type': 'text', 'text': "I'll list the Python files in the current directory for you."}, {'type': 'tool_call', 'name': 'bash', 'args': {'command': 'ls -la *.py'}, 'id': 'toolu_01ABC123...'}]
The bash tool supports two parameters:
command (required): The bash command to execute
restart (optional): Set to true to restart the bash session
Claude can use a code execution tool to execute code in a sandboxed environment.
Anthropic’s 2025-08-25 code execution tools are supported since langchain-anthropic>=1.0.3.The legacy 2025-05-22 tool is supported since langchain-anthropic>=0.3.14.
The code sandbox does not have internet access, thus you may only use packages that are pre-installed in the environment. See the Claude docs for more info.
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic( model="claude-sonnet-4-5-20250929",)tool = {"type": "code_execution_20250825", "name": "code_execution"} model_with_tools = model.bind_tools([tool])response = model_with_tools.invoke( "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]")
Use with Files API
Using the Files API, Claude can write code to access files for data analysis and other purposes. See example below:
Copy
import anthropicfrom langchain_anthropic import ChatAnthropicclient = anthropic.Anthropic()file = client.beta.files.upload( file=open("/path/to/sample_data.csv", "rb"))file_id = file.id# Run inferencemodel = ChatAnthropic( model="claude-sonnet-4-5-20250929",)tool = {"type": "code_execution_20250825", "name": "code_execution"} model_with_tools = model.bind_tools([tool])input_message = { "role": "user", "content": [ { "type": "text", "text": "Please plot these data and tell me what you see.", }, { "type": "container_upload", "file_id": file_id, }, ]}response = model_with_tools.invoke([input_message])
Note that Claude may generate files as part of its code execution. You can access these files using the Files API:
Copy
# Take all file outputs for demonstration purposesfile_ids = []for block in response.content: if block["type"] == "bash_code_execution_tool_result": file_ids.extend( content["file_id"] for content in block.get("content", {}).get("content", []) if "file_id" in content )for i, file_id in enumerate(file_ids): file_content = client.beta.files.download(file_id) file_content.write_to_file(f"/path/to/file_{i}.png")
Claude supports computer use capabilities, allowing it to interact with desktop environments through screenshots, mouse control, and keyboard input.
Important: You must provide the execution environmentLangChain handles the API integration (sending/receiving tool calls), but you are responsible for:
Setting up a sandboxed computing environment (Linux VM, Docker container, etc.)
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")# LangChain handles the API call and tool bindingcomputer_tool = { "type": "computer_20250124", "name": "computer", "display_width_px": 1024, "display_height_px": 768, "display_number": 1,}model_with_computer = model.bind_tools([computer_tool])response = model_with_computer.invoke( "Take a screenshot to see what's on the screen")
response.tool_calls will contain the computer action Claude wants to perform. You must execute this action in your environment and pass the result back.
Copy
[{'type': 'text', 'text': "I'll take a screenshot to see what's currently on the screen."}, {'type': 'tool_call', 'name': 'computer', 'args': {'action': 'screenshot'}, 'id': 'toolu_01RNsqAE7dDZujELtacNeYv9'}]
Available tool versions:
computer_20250124 (for Claude 4 and Claude Sonnet 3.7)
The text editor tool can be used to view and modify text files. See docs here for details.
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")tool = {"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}model_with_tools = model.bind_tools([tool])response = model_with_tools.invoke( "There's a syntax error in my primes.py file. Can you help me fix it?")print(response.text)response.tool_calls
Copy
I'll help you fix the syntax error in your primes.py file. Let me first take a look at the file to identify the issue.
Claude can use a web search tool to run searches and ground its responses with citations.
Web search tool is supported since langchain-anthropic>=0.3.13
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic(model="claude-sonnet-4-5-20250929")tool = {"type": "web_search_20250305", "name": "web_search", "max_uses": 3} model_with_tools = model.bind_tools([tool])response = model_with_tools.invoke("How do I update a web app to TypeScript 5.5?")
Claude supports a memory tool for client-side storage and retrieval of context across conversational threads. See docs here for details.
Anthropic’s built-in memory tool is supported since langchain-anthropic>=0.3.21
Copy
from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic( model="claude-sonnet-4-5-20250929",)model_with_tools = model.bind_tools([{"type": "memory_20250818", "name": "memory"}]) response = model_with_tools.invoke("What are my interests?")response.content_blocks
Copy
[{'type': 'text', 'text': "I'll check my memory to see what information I have about your interests."}, {'type': 'tool_call', 'name': 'memory', 'args': {'command': 'view', 'path': '/memories'}, 'id': 'toolu_01XeP9sxx44rcZHFNqXSaKqh'}]
Claude supports a tool search feature that enables dynamic tool discovery and loading. Instead of loading all tool definitions into the context window upfront, Claude can search your tool catalog and load only the tools it needs.This is useful when:
You have 10+ tools available in your system
Tool definitions are consuming significant tokens
You’re experiencing tool selection accuracy issues with large tool sets
There are two tool search variants:
Regex (tool_search_tool_regex_20251119): Claude constructs regex patterns to search for tools
BM25 (tool_search_tool_bm25_20251119): Claude uses natural language queries to search for tools
Use the extras parameter to specify defer_loading on LangChain tools:
Copy
from langchain_anthropic import ChatAnthropicfrom langchain.tools import tool@tool(extras={"defer_loading": True}) def get_weather(location: str, unit: str = "fahrenheit") -> str: """Get the current weather for a location. Args: location: City name unit: Temperature unit (celsius or fahrenheit) """ return f"Weather in {location}: Sunny"@tool(extras={"defer_loading": True}) def search_files(query: str) -> str: """Search through files in the workspace. Args: query: Search query """ return f"Found files matching '{query}'"model = ChatAnthropic(model="claude-sonnet-4-5-20250929")model_with_tools = model.bind_tools([ {"type": "tool_search_tool_regex_20251119", "name": "tool_search_tool_regex"}, get_weather, search_files,])response = model_with_tools.invoke("What's the weather in San Francisco?")
Key points:
Tools with defer_loading: True are only loaded when Claude discovers them via search
Keep your 3-5 most frequently used tools as non-deferred for optimal performance
Both variants search tool names, descriptions, argument names, and argument descriptions
See the Claude documentation for more details on tool search, including usage with MCP servers and client-side implementations.