Skip to content

The agent engineering platform.

docs(anthropic,core,langchain): updates (#34106)

Mason Daugherty0a6d01e61df8
diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py
index c03086acb9..a21aba27c8 100644
--- a/libs/core/langchain_core/language_models/chat_models.py
+++ b/libs/core/langchain_core/language_models/chat_models.py
@@ -1632,7 +1632,7 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC):
# }
```
- Example: `dict` schema (`include_raw=False`):
+ Example: Dictionary schema (`include_raw=False`):
```python
from pydantic import BaseModel
diff --git a/libs/core/langchain_core/messages/content.py b/libs/core/langchain_core/messages/content.py
index 568579bc20..c49dc770eb 100644
--- a/libs/core/langchain_core/messages/content.py
+++ b/libs/core/langchain_core/messages/content.py
@@ -654,7 +654,7 @@ class PlainTextContentBlock(TypedDict):
!!! note
Title and context are optional fields that may be passed to the model. See
- Anthropic [example](https://docs.claude.com/en/docs/build-with-claude/citations#citable-vs-non-citable-content).
+ Anthropic [example](https://platform.claude.com/docs/en/build-with-claude/citations#citable-vs-non-citable-content).
!!! note "Factory function"
`create_plaintext_block` may also be used as a factory to create a
diff --git a/libs/langchain_v1/langchain/agents/middleware/context_editing.py b/libs/langchain_v1/langchain/agents/middleware/context_editing.py
index 538b6ee567..0f13221010 100644
--- a/libs/langchain_v1/langchain/agents/middleware/context_editing.py
+++ b/libs/langchain_v1/langchain/agents/middleware/context_editing.py
@@ -189,7 +189,7 @@ class ContextEditingMiddleware(AgentMiddleware):
configured thresholds.
Currently the `ClearToolUsesEdit` strategy is supported, aligning with Anthropic's
- `clear_tool_uses_20250919` behavior [(read more)](https://docs.claude.com/en/docs/agents-and-tools/tool-use/memory-tool).
+ `clear_tool_uses_20250919` behavior [(read more)](https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool).
"""
edits: list[ContextEdit]
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index 429e215a23..aa95caa841 100644
--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py
+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py
@@ -96,7 +96,7 @@ _FALLBACK_MAX_OUTPUT_TOKENS: Final[int] = 4096
def _default_max_tokens_for(model: str | None) -> int:
"""Return the default max output tokens for an Anthropic model (with fallback).
- See the Claude docs for [Max Tokens limits](https://docs.claude.com/en/docs/about-claude/models/overview#model-comparison-table).
+ See the Claude docs for [Max Tokens limits](https://platform.claude.com/docs/en/about-claude/models/overview#model-comparison-table).
"""
if not model:
return _FALLBACK_MAX_OUTPUT_TOKENS
@@ -124,7 +124,7 @@ class AnthropicTool(TypedDict):
def _is_builtin_tool(tool: Any) -> bool:
"""Check if a tool is a built-in Anthropic tool.
- [Claude docs for built-in tools](https://docs.claude.com/en/docs/agents-and-tools/tool-use/overview)
+ [Claude docs for built-in tools](https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview)
"""
if not isinstance(tool, dict):
return False
@@ -593,10 +593,10 @@ def _handle_anthropic_bad_request(e: anthropic.BadRequestError) -> None:
class ChatAnthropic(BaseChatModel):
- """Anthropic chat models.
+ """Anthropic (Claude) chat models.
- See [Anthropic's docs](https://docs.claude.com/en/docs/about-claude/models/overview)
- for a list of the latest models.
+ See the [Claude Platform docs](https://platform.claude.com/docs/en/about-claude/models/overview)
+ for a list of the latest models, their capabilities, and pricing.
Setup:
Install `langchain-anthropic` and set environment variable `ANTHROPIC_API_KEY`.
@@ -606,32 +606,35 @@ class ChatAnthropic(BaseChatModel):
export ANTHROPIC_API_KEY="your-api-key"
```
- Key init args — completion params:
- model:
- Name of Anthropic model to use. e.g. `'claude-sonnet-4-5-20250929'`.
- temperature:
+ Key init args:
+ **Completion params:**
+
+ * [`model`][langchain_anthropic.chat_models.ChatAnthropic.model]: Name of
+ Anthropic model to use. e.g. `'claude-sonnet-4-5-20250929'`.
+ * [`temperature`][langchain_anthropic.chat_models.ChatAnthropic.temperature]:
Sampling temperature. Ranges from `0.0` to `1.0`.
- max_tokens:
- Max number of tokens to generate.
+ * [`max_tokens`][langchain_anthropic.chat_models.ChatAnthropic.max_tokens]: Max
+ number of tokens to generate.
+
+ **Client params:**
- Key init args — client params:
- timeout:
+ * [`timeout`][langchain_anthropic.chat_models.ChatAnthropic.default_request_timeout]:
Timeout for requests.
- anthropic_proxy:
+ * [`anthropic_proxy`][langchain_anthropic.chat_models.ChatAnthropic.anthropic_proxy]:
Proxy to use for the Anthropic clients, will be used for every API call.
If not passed in will be read from env var `ANTHROPIC_PROXY`.
- max_retries:
+ * [`max_retries`][langchain_anthropic.chat_models.ChatAnthropic.max_retries]:
Max number of retries if a request fails.
- api_key:
+ * [`api_key`][langchain_anthropic.chat_models.ChatAnthropic.anthropic_api_key]:
Anthropic API key. If not passed in will be read from env var
`ANTHROPIC_API_KEY`.
- base_url:
- Base URL for API requests. Only specify if using a proxy or service
- emulator.
+ * [`base_url`][langchain_anthropic.chat_models.ChatAnthropic.anthropic_api_url]:
+ Base URL for API requests. Only specify if using a proxy or service emulator.
- See full list of supported init args and their descriptions in the params section.
+ See full list of supported init args and their descriptions below.
+
+ ???+ example "Instantiate"
- Instantiate:
```python
from langchain_anthropic import ChatAnthropic
@@ -647,34 +650,40 @@ class ChatAnthropic(BaseChatModel):
)
```
- !!! note
- Any param which is not explicitly supported will be passed directly to the
- `anthropic.Anthropic.messages.create(...)` API every time to the model is
- invoked. For example:
+ ???+ note
- ```python
- from langchain_anthropic import ChatAnthropic
- import anthropic
+ Any param which is not explicitly supported will be passed directly to
+ `Anthropic.messages.create(...)` each time to the model is invoked.
- ChatAnthropic(..., extra_headers={}).invoke(...)
+ !!! example
- # results in underlying API call of:
+ ```python
+ from langchain_anthropic import ChatAnthropic
+ import anthropic
- anthropic.Anthropic(..).messages.create(..., extra_headers={})
+ ChatAnthropic(..., extra_headers={}).invoke(...)
- # which is also equivalent to:
+ # Results in underlying API call of:
- ChatAnthropic(...).invoke(..., extra_headers={})
- ```
+ anthropic.Anthropic(..).messages.create(..., extra_headers={})
+
+ # ... which is also equivalent to:
+
+ ChatAnthropic(...).invoke(..., extra_headers={})
+ ```
+
+ ???+ example "Invoke"
- Invoke:
```python
messages = [
(
"system",
"You are a helpful translator. Translate the user sentence to French.",
),
- ("human", "I love programming."),
+ (
+ "human",
+ "I love programming.",
+ ),
]
model.invoke(messages)
```
@@ -698,7 +707,8 @@ class ChatAnthropic(BaseChatModel):
)
```
- Stream:
+ ???+ example "Stream"
+
```python
for chunk in model.stream(messages):
print(chunk.text, end="")
@@ -715,6 +725,8 @@ class ChatAnthropic(BaseChatModel):
AIMessageChunk(content=".", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
```
+ To aggregate the full message from the stream:
+
```python
stream = model.stream(messages)
full = next(stream)
@@ -727,7 +739,8 @@ class ChatAnthropic(BaseChatModel):
AIMessageChunk(content="J'aime la programmation.", id="run-b34faef0-882f-4869-a19c-ed2b856e6361")
```
- Async:
+ ???+ example "Async invocation"
+
```python
await model.ainvoke(messages)
@@ -757,8 +770,9 @@ class ChatAnthropic(BaseChatModel):
)
```
- Tool calling:
- ```python
+ ???+ example "Tool calling"
+
+ ```python hl_lines="16"
from pydantic import BaseModel, Field
@@ -804,39 +818,57 @@ class ChatAnthropic(BaseChatModel):
]
```
- See `ChatAnthropic.bind_tools()` method for more.
+ See [`ChatAnthropic.bind_tools()`][langchain_anthropic.chat_models.ChatAnthropic.bind_tools]
+ for more info.
- Structured output:
- ```python
- from typing import Optional
+ !!! note "Strict tool use"
- from pydantic import BaseModel, Field
+ Anthropic supports a strict tool use feature that guarantees tool names
+ and arguments are validated and correctly typed.
+ See [`ChatAnthropic.bind_tools()`][langchain_anthropic.chat_models.ChatAnthropic.bind_tools]
+ for more info.
- class Joke(BaseModel):
- '''Joke to tell user.'''
+ ???+ example "Token-efficient tool use (beta)"
- setup: str = Field(description="The setup of the joke")
- punchline: str = Field(description="The punchline to the joke")
- rating: int | None = Field(description="How funny the joke is, from 1 to 10")
+ See LangChain [docs](https://docs.langchain.com/oss/python/integrations/chat/anthropic#token-efficient-tool-use)
+ for more detail.
+ ```python hl_lines="9"
+ from langchain_anthropic import ChatAnthropic
+ from langchain_core.tools import tool
- structured_model = model.with_structured_output(Joke)
- structured_model.invoke("Tell me a joke about cats")
- ```
+ model = ChatAnthropic(
+ model="claude-sonnet-4-5-20250929",
+ temperature=0,
+ model_kwargs={
+ "extra_headers": {
+ "anthropic-beta": "token-efficient-tools-2025-02-19"
+ }
+ }
+ )
- ```python
- Joke(
- setup="Why was the cat sitting on the computer?",
- punchline="To keep an eye on the mouse!",
- rating=None,
+ @tool
+ def get_weather(location: str) -> str:
+ \"\"\"Get the weather at a location.\"\"\"
+ return "It's sunny."
+
+ model_with_tools = model.bind_tools([get_weather])
+ response = model_with_tools.invoke(
+ "What's the weather in San Francisco?"
)
+ print(response.tool_calls)
+ print(f'Total tokens: {response.usage_metadata["total_tokens"]}')
```
- See `ChatAnthropic.with_structured_output()` for more.
+ ```txt
+ [{'name': 'get_weather', 'args': {'location': 'San Francisco'}, 'id': 'toolu_01HLjQMSb1nWmgevQUtEyz17', 'type': 'tool_call'}]
+ Total tokens: 408
+ ```
- Image input:
- See [multimodal guides](https://docs.langchain.com/oss/python/langchain/models#multimodal)
+ ???+ example "Image input"
+
+ See the [multimodal guide](https://docs.langchain.com/oss/python/langchain/models#multimodal)
for more detail.
```python
@@ -875,10 +907,10 @@ class ChatAnthropic(BaseChatModel):
"After examining both images carefully, I can see that they are actually identical."
```
- ??? note "Files API"
+ ??? example "Upload with Files API"
You can also pass in files that are managed through Anthropic's
- [Files API](https://docs.claude.com/en/docs/build-with-claude/files):
+ [Files API](https://platform.claude.com/docs/en/build-with-claude/files):
```python
from langchain_anthropic import ChatAnthropic
@@ -903,8 +935,9 @@ class ChatAnthropic(BaseChatModel):
model.invoke([input_message])
```
- PDF input:
- See [multimodal guides](https://docs.langchain.com/oss/python/langchain/models#multimodal)
+ ???+ example "PDF input"
+
+ See the [multimodal guide](https://docs.langchain.com/oss/python/langchain/models#multimodal)
for more detail.
```python
@@ -938,10 +971,10 @@ class ChatAnthropic(BaseChatModel):
"This appears to be a simple document..."
```
- ??? note "Files API"
+ ??? example "Upload with Files API"
You can also pass in files that are managed through Anthropic's
- [Files API](https://docs.claude.com/en/docs/build-with-claude/files):
+ [Files API](https://platform.claude.com/docs/en/build-with-claude/files):
```python
from langchain_anthropic import ChatAnthropic
@@ -966,9 +999,10 @@ class ChatAnthropic(BaseChatModel):
model.invoke([input_message])
```
- Extended thinking:
- Certain [Claude models](https://docs.claude.com/en/docs/build-with-claude/extended-thinking#supported-models)
- support an [extended thinking](https://docs.claude.com/en/docs/build-with-claude/extended-thinking)
+ ???+ example "Extended thinking"
+
+ Certain [Claude models](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#supported-models)
+ support an [extended thinking](https://platform.claude.com/docs/en/build-with-claude/extended-thinking)
feature, which will output the step-by-step reasoning process that led to its
final answer.
@@ -976,146 +1010,53 @@ class ChatAnthropic(BaseChatModel):
It can also be passed in as a kwarg during invocation.
- You will need to specify a token budget to use this feature. See usage example:
-
- ```python
- from langchain_anthropic import ChatAnthropic
-
- model = 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?")
- response.content
- ```
-
- ```python
- [
- {
- "signature": "...",
- "thinking": "To find the cube root of 50.653...",
- "type": "thinking",
- },
- {"text": "The cube root of 50.653 is ...", "type": "text"},
- ]
- ```
+ **You will need to specify a token budget** to use this feature.
- !!! warning "Differences in thinking across model versions"
- The Claude Messages API handles thinking differently across Claude Sonnet
- 3.7 and Claude 4 models. Refer to [their docs](https://docs.claude.com/en/docs/build-with-claude/extended-thinking#differences-in-thinking-across-model-versions)
- for more info.
+ !!! example
- Citations:
- Anthropic supports a [citations](https://docs.claude.com/en/docs/build-with-claude/citations)
- feature that lets Claude attach context to its answers based on source
- documents supplied by the user. When [document content blocks](https://docs.claude.com/en/docs/build-with-claude/citations#document-types)
- with `"citations": {"enabled": True}` are included in a query, Claude may
- generate citations in its response.
-
- ```python
- from langchain_anthropic import ChatAnthropic
-
- model = ChatAnthropic(model="claude-3-5-haiku-20241022")
-
- 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
- ```
+ ```python hl_lines="5-6"
+ from langchain_anthropic import ChatAnthropic
- ```python
- [
- {"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"},
- ]
- ```
+ model = ChatAnthropic(
+ model="claude-sonnet-4-5-20250929",
+ max_tokens=5000,
+ thinking={"type": "enabled", "budget_tokens": 2000},
+ )
- Token usage:
- ```python
- ai_msg = model.invoke(messages)
- ai_msg.usage_metadata
- ```
+ response = model.invoke("What is the cube root of 50.653?")
+ response.content
+ ```
- ```python
- {"input_tokens": 25, "output_tokens": 11, "total_tokens": 36}
- ```
+ ```python
+ [
+ {
+ "signature": "...",
+ "thinking": "To find the cube root of 50.653...",
+ "type": "thinking",
+ },
+ {"text": "The cube root of 50.653 is ...", "type": "text"},
+ ]
+ ```
- Message chunks containing token usage will be included during streaming by
- default:
+ !!! warning "Differences in thinking across model versions"
- ```python
- stream = model.stream(messages)
- full = next(stream)
- for chunk in stream:
- full += chunk
- full.usage_metadata
- ```
+ The Claude Messages API handles thinking differently across Claude Sonnet
+ 3.7 and Claude 4 models.
- ```python
- {"input_tokens": 25, "output_tokens": 11, "total_tokens": 36}
- ```
+ Refer to the [Claude docs](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#differences-in-thinking-across-model-versions)
+ for more info.
- These can be disabled by setting `stream_usage=False` in the stream method,
- or by setting `stream_usage=False` when initializing ChatAnthropic.
+ ???+ example "Prompt caching"
- Prompt caching:
Prompt caching reduces processing time and costs for repetitive tasks or prompts
with consistent elements
!!! note
Only certain models support prompt caching.
- See the [Claude documentation](https://docs.claude.com/en/docs/build-with-claude/prompt-caching#supported-models)
+ See the [Claude documentation](https://platform.claude.com/docs/en/build-with-claude/prompt-caching#supported-models)
for a full list.
- ```python
+ ```python hl_lines="16"
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
@@ -1151,22 +1092,22 @@ class ChatAnthropic(BaseChatModel):
Alternatively, you may enable prompt caching at invocation time. You may want to
conditionally cache based on runtime conditions, such as the length of the
- context. Alternatively, this is useful for app-level decisions about what to
+ context. This is useful for app-level decisions about what to
cache.
- ```python
+ ```python hl_lines="3"
response = model.invoke(
messages,
cache_control={"type": "ephemeral"},
)
```
- ??? note "Extended caching"
+ ??? example "Extended caching"
The cache lifetime is 5 minutes by default. If this is too short, you can
apply one hour caching by setting `ttl` to `'1h'`.
- ```python
+ ```python hl_lines="12"
model = ChatAnthropic(
model="claude-sonnet-4-5-20250929",
)
@@ -1209,15 +1150,155 @@ class ChatAnthropic(BaseChatModel):
}
```
- See [Claude documentation](https://docs.claude.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration-beta)
+ See [Claude documentation](https://platform.claude.com/docs/en/build-with-claude/prompt-caching#1-hour-cache-duration-beta)
for detail.
- !!! note "Extended context windows (beta)"
+ ???+ example "Token usage metadata"
+
+ ```python
+ ai_msg = model.invoke(messages)
+ ai_msg.usage_metadata
+ ```
+
+ ```python
+ {"input_tokens": 25, "output_tokens": 11, "total_tokens": 36}
+ ```
+
+ Message chunks containing token usage will be included during streaming by
+ default:
+
+ ```python
+ stream = model.stream(messages)
+ full = next(stream)
+ for chunk in stream:
+ full += chunk
+ full.usage_metadata
+ ```
+
+ ```python
+ {"input_tokens": 25, "output_tokens": 11, "total_tokens": 36}
+ ```
+
+ These can be disabled by setting [`stream_usage=False`][langchain_anthropic.chat_models.ChatAnthropic.stream_usage]
+ in the stream method or when initializing `ChatAnthropic`.
+
+ ???+ example "Citations"
+
+ Anthropic supports a [citations](https://platform.claude.com/docs/en/build-with-claude/citations)
+ feature that lets Claude attach context to its answers based on source
+ documents supplied by the user.
+
+ When passing a [Claude document content block](https://platform.claude.com/docs/en/build-with-claude/citations#document-types)
+ with `#!json "citations": {"enabled": True}` included in the query, Claude may
+ generate citations in its response.
+
+ ```python hl_lines="9-19"
+ from langchain_anthropic import ChatAnthropic
+
+ model = ChatAnthropic(model="claude-3-5-haiku-20241022")
+
+ 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
+ ```
+
+ ```python hl_lines="6-15 21-30"
+ [
+ {"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"},
+ ]
+ ```
+
+ ???+ example "Context management"
+
+ Anthropic supports a context editing feature that will automatically manage the
+ model's context window (e.g., by clearing tool results).
+
+ See [Anthropic documentation](https://platform.claude.com/docs/en/build-with-claude/context-editing)
+ for details and configuration options.
+
+ ```python hl_lines="5-6"
+ from langchain_anthropic import ChatAnthropic
+
+ model = 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")
+ ```
+
+ ???+ example "Response metadata"
+
+ ```python
+ ai_msg = model.invoke(messages)
+ ai_msg.response_metadata
+ ```
+
+ ```python
+ {
+ "id": "msg_013xU6FHEGEq76aP4RgFerVT",
+ "model": "claude-sonnet-4-5-20250929",
+ "stop_reason": "end_turn",
+ "stop_sequence": None,
+ "usage": {"input_tokens": 25, "output_tokens": 11},
+ }
+ ```
+
+ ???+ example "Extended context windows (beta)"
Claude Sonnet 4 supports a 1-million token context window, available in beta for
organizations in usage tier 4 and organizations with custom rate limits.
- ```python
+ ```python hl_lines="5"
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(
@@ -1244,75 +1325,55 @@ class ChatAnthropic(BaseChatModel):
response = model.invoke(messages)
```
- See [Claude documentation](https://docs.claude.com/en/docs/build-with-claude/context-windows#1m-token-context-window)
+ See [Claude documentation](https://platform.claude.com/docs/en/build-with-claude/context-windows#1m-token-context-window)
for detail.
+ ???+ example "Structured output"
- !!! note "Token-efficient tool use (beta)"
+ ```python hl_lines="13"
+ from typing import Optional
+ from pydantic import BaseModel, Field
- See LangChain [docs](https://docs.langchain.com/oss/python/integrations/chat/anthropic)
- for more detail.
- ```python
- from langchain_anthropic import ChatAnthropic
- from langchain_core.tools import tool
+ class Joke(BaseModel):
+ '''Joke to tell user.'''
- model = ChatAnthropic(
- model="claude-sonnet-4-5-20250929",
- temperature=0,
- model_kwargs={
- "extra_headers": {
- "anthropic-beta": "token-efficient-tools-2025-02-19"
- }
- }
- )
+ setup: str = Field(description="The setup of the joke")
+ punchline: str = Field(description="The punchline to the joke")
+ rating: int | None = Field(description="How funny the joke is, from 1 to 10")
- @tool
- def get_weather(location: str) -> str:
- \"\"\"Get the weather at a location.\"\"\"
- return "It's sunny."
- model_with_tools = model.bind_tools([get_weather])
- response = model_with_tools.invoke(
- "What's the weather in San Francisco?"
- )
- print(response.tool_calls)
- print(f'Total tokens: {response.usage_metadata["total_tokens"]}')
+ structured_model = model.with_structured_output(Joke)
+ structured_model.invoke("Tell me a joke about cats")
```
- ```txt
- [{'name': 'get_weather', 'args': {'location': 'San Francisco'}, 'id': 'toolu_01HLjQMSb1nWmgevQUtEyz17', 'type': 'tool_call'}]
- Total tokens: 408
+ ```python
+ Joke(
+ setup="Why was the cat sitting on the computer?",
+ punchline="To keep an eye on the mouse!",
+ rating=None,
+ )
```
- !!! note "Context management"
+ See [`ChatAnthropic.with_structured_output()`][langchain_anthropic.chat_models.ChatAnthropic.with_structured_output]
+ for more info.
- Anthropic supports a context editing feature that will automatically manage the
- model's context window (e.g., by clearing tool results).
+ !!! note "Native structured output"
- See [Anthropic documentation](https://docs.claude.com/en/docs/build-with-claude/context-editing)
- for details and configuration options.
-
- ```python
- from langchain_anthropic import ChatAnthropic
+ Anthropic supports a native structured output feature that guarantees
+ responses adhere to a given schema.
- model = 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")
- ```
+ See [`ChatAnthropic.with_structured_output()`][langchain_anthropic.chat_models.ChatAnthropic.with_structured_output]
+ for more info.
- !!! note "Built-in tools"
+ ???+ example "Built-in tools"
See LangChain [docs](https://docs.langchain.com/oss/python/integrations/chat/anthropic#built-in-tools)
for more detail.
- ??? note "Web search"
+ ??? example "Web search"
- ```python
+ ```python hl_lines="5-9"
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(model="claude-3-5-haiku-20241022")
@@ -1327,9 +1388,12 @@ class ChatAnthropic(BaseChatModel):
response = model_with_tools.invoke("How do I update a web app to TypeScript 5.5?")
```
- ??? note "Web fetch (beta)"
+ See the [Claude docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-search-tool)
+ for more info.
+
+ ??? example "Web fetch (beta)"
- ```python
+ ```python hl_lines="5 8-12"
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(
@@ -1347,15 +1411,21 @@ class ChatAnthropic(BaseChatModel):
response = model_with_tools.invoke("Please analyze the content at https://example.com/article")
```
- ??? note "Code execution"
+ See the [Claude docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-fetch-tool)
+ for more info.
- ```python
+ ??? example "Code execution"
+
+ ```python hl_lines="3 6-9"
model = ChatAnthropic(
model="claude-sonnet-4-5-20250929",
- betas=["code-execution-2025-05-22"],
+ betas=["code-execution-2025-05-22"], # Enable code execution beta
)
- tool = {"type": "code_execution_20250522", "name": "code_execution"}
+ tool = {
+ "type": "code_execution_20250522",
+ "name": "code_execution",
+ }
model_with_tools = model.bind_tools([tool])
response = model_with_tools.invoke(
@@ -1363,9 +1433,34 @@ class ChatAnthropic(BaseChatModel):
)
```
- ??? note "Remote MCP"
+ See the [Claude docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool)
+ for more info.
- ```python
+ ??? example "Memory tool"
+
+ ```python hl_lines="5 8-11"
+ from langchain_anthropic import ChatAnthropic
+
+ model = ChatAnthropic(
+ model="claude-sonnet-4-5-20250929",
+ betas=["context-management-2025-06-27"], # Enable context management beta
+ )
+
+ tool = {
+ "type": "memory_20250818",
+ "name": "memory",
+ }
+ model_with_tools = model.bind_tools([tool])
+
+ response = model_with_tools.invoke("What are my interests?")
+ ```
+
+ See the [Claude docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool)
+ for more info.
+
+ ??? example "Remote MCP"
+
+ ```python hl_lines="3-14 18-19"
from langchain_anthropic import ChatAnthropic
mcp_servers = [
@@ -1383,8 +1478,8 @@ class ChatAnthropic(BaseChatModel):
model = ChatAnthropic(
model="claude-sonnet-4-5-20250929",
- betas=["mcp-client-2025-04-04"],
- mcp_servers=mcp_servers,
+ betas=["mcp-client-2025-04-04"], # Enable MCP client beta
+ mcp_servers=mcp_servers, # Pass in MCP server configurations
)
response = model.invoke(
@@ -1393,14 +1488,20 @@ class ChatAnthropic(BaseChatModel):
)
```
- ??? note "Text editor"
+ See the [Claude docs](https://platform.claude.com/docs/en/agents-and-tools/mcp-connector)
+ for more info.
+
+ ??? example "Text editor"
- ```python
+ ```python hl_lines="5-8"
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
- tool = {"type": "text_editor_20250124", "name": "str_replace_editor"}
+ tool = {
+ "type": "text_editor_20250124",
+ "name": "str_replace_editor",
+ }
model_with_tools = model.bind_tools([tool])
response = model_with_tools.invoke(
@@ -1412,42 +1513,17 @@ class ChatAnthropic(BaseChatModel):
```txt
I'd be happy to help you fix the syntax error in your primes.py file. First, let's look at the current content of the file to identify the error.
+ ```
+ ```txt
[{'name': 'str_replace_editor',
'args': {'command': 'view', 'path': '/repo/primes.py'},
'id': 'toolu_01VdNgt1YV7kGfj9LFLm6HyQ',
'type': 'tool_call'}]
```
- ??? note "Memory tool"
-
- ```python
- from langchain_anthropic import ChatAnthropic
-
- model = ChatAnthropic(
- model="claude-sonnet-4-5-20250929",
- betas=["context-management-2025-06-27"],
- )
- model_with_tools = model.bind_tools([{"type": "memory_20250818", "name": "memory"}])
- response = model_with_tools.invoke("What are my interests?")
- ```
-
- !!! note "Response metadata"
-
- ```python
- ai_msg = model.invoke(messages)
- ai_msg.response_metadata
- ```
-
- ```python
- {
- "id": "msg_013xU6FHEGEq76aP4RgFerVT",
- "model": "claude-sonnet-4-5-20250929",
- "stop_reason": "end_turn",
- "stop_sequence": None,
- "usage": {"input_tokens": 25, "output_tokens": 11},
- }
- ```
+ See the [Claude docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/text-editor-tool)
+ for more info.
""" # noqa: E501
model_config = ConfigDict(
@@ -1470,11 +1546,11 @@ class ChatAnthropic(BaseChatModel):
"""Total probability mass of tokens to consider at each step."""
default_request_timeout: float | None = Field(None, alias="timeout")
- """Timeout for requests to Anthropic Completion API."""
+ """Timeout for requests to Claude API."""
# sdk default = 2: https://github.com/anthropics/anthropic-sdk-python?tab=readme-ov-file#retries
max_retries: int = 2
- """Number of retries allowed for requests sent to the Anthropic Completion API."""
+ """Number of retries allowed for requests sent to the Claude API."""
stop_sequences: list[str] | None = Field(None, alias="stop")
"""Default stop sequences."""
@@ -1490,8 +1566,6 @@ class ChatAnthropic(BaseChatModel):
If a value isn't passed in, will attempt to read the value first from
`ANTHROPIC_API_URL` and if that is not set, `ANTHROPIC_BASE_URL`.
- If neither are set, the default value of `https://api.anthropic.com` will
- be used.
"""
anthropic_api_key: SecretStr = Field(
@@ -1506,16 +1580,17 @@ class ChatAnthropic(BaseChatModel):
"""Proxy to use for the Anthropic clients, will be used for every API call.
If not provided, will attempt to read from the `ANTHROPIC_PROXY` environment
- variable."""
+ variable.
+ """
default_headers: Mapping[str, str] | None = None
"""Headers to pass to the Anthropic clients, will be used for every API call."""
betas: list[str] | None = None
"""List of beta features to enable. If specified, invocations will be routed
- through client.beta.messages.create.
+ through `client.beta.messages.create`.
- Example: `betas=["mcp-client-2025-04-04"]`
+ Example: `#!python betas=["mcp-client-2025-04-04"]`
"""
model_kwargs: dict[str, Any] = Field(default_factory=dict)
@@ -1524,24 +1599,38 @@ class ChatAnthropic(BaseChatModel):
"""Whether to use streaming or not."""
stream_usage: bool = True
- """Whether to include usage metadata in streaming output. If `True`, additional
- message chunks will be generated during the stream including usage metadata.
+ """Whether to include usage metadata in streaming output.
+
+ If `True`, additional message chunks will be generated during the stream including
+ usage metadata.
"""
thinking: dict[str, Any] | None = Field(default=None)
"""Parameters for Claude reasoning,
- e.g., `{"type": "enabled", "budget_tokens": 10_000}`"""
+
+ e.g., `#!python {"type": "enabled", "budget_tokens": 10_000}`
+ """
mcp_servers: list[dict[str, Any]] | None = None
"""List of MCP servers to use for the request.
- Example: `mcp_servers=[{"type": "url", "url": "https://mcp.example.com/mcp",
+ Example: `#!python mcp_servers=[{"type": "url", "url": "https://mcp.example.com/mcp",
"name": "example-mcp"}]`
+
+ !!! note
+
+ This feature requires the beta header `'mcp-client-2025-11-20'` to be set in
+ [`betas`][langchain_anthropic.chat_models.ChatAnthropic.betas].
"""
context_management: dict[str, Any] | None = None
"""Configuration for
- [context management](https://docs.claude.com/en/docs/build-with-claude/context-editing).
+ [context management](https://platform.claude.com/docs/en/build-with-claude/context-editing).
+
+ !!! note
+
+ This feature requires the beta header `'context-management-2025-06-27'` to be
+ set in [`betas`][langchain_anthropic.chat_models.ChatAnthropic.betas].
"""
@property
@@ -1609,7 +1698,7 @@ class ChatAnthropic(BaseChatModel):
@model_validator(mode="before")
@classmethod
def set_default_max_tokens(cls, values: dict[str, Any]) -> Any:
- """Set default max_tokens."""
+ """Set default `max_tokens`."""
if values.get("max_tokens") is None:
model = values.get("model") or values.get("model_name")
values["max_tokens"] = _default_max_tokens_for(model)
@@ -1972,22 +2061,25 @@ class ChatAnthropic(BaseChatModel):
Args:
tools: A list of tool definitions to bind to this chat model.
+
Supports Anthropic format tool schemas and any tool definition handled
by `langchain_core.utils.function_calling.convert_to_openai_tool`.
tool_choice: Which tool to require the model to call. Options are:
- - name of the tool as a string or as dict `{"type": "tool", "name": "<<tool_name>>"}`: calls corresponding tool;
- - `'auto'`, `{"type: "auto"}`, or `None`: automatically selects a tool (including no tool);
- - `'any'` or `{"type: "any"}`: force at least one tool to be called;
+ - Name of the tool as a string or as dict `{"type": "tool", "name": "<<tool_name>>"}`: calls corresponding tool
+ - `'auto'`, `{"type: "auto"}`, or `None`: automatically selects a tool (including no tool)
+ - `'any'` or `{"type: "any"}`: force at least one tool to be called
parallel_tool_calls: Set to `False` to disable parallel tool use.
+
Defaults to `None` (no specification, which allows parallel tool use).
!!! version-added "Added in `langchain-anthropic` 0.3.2"
strict: If `True`, Claude's schema adherence is applied to tool calls.
- See: [Anthropic docs](https://docs.claude.com/en/docs/build-with-claude/structured-outputs#when-to-use-json-outputs-vs-strict-tool-use).
+
+ See the [Claude docs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs#when-to-use-json-outputs-vs-strict-tool-use).
kwargs: Any additional parameters are passed directly to `bind`.
- Example:
+ ???+ example
```python
from langchain_anthropic import ChatAnthropic
from pydantic import BaseModel, Field
@@ -2020,7 +2112,7 @@ class ChatAnthropic(BaseChatModel):
# )
```
- Example — force tool call with tool_choice `'any'`:
+ ??? example "Force tool call with tool_choice `'any'`"
```python
from langchain_anthropic import ChatAnthropic
@@ -2046,156 +2138,190 @@ class ChatAnthropic(BaseChatModel):
)
```
- Example — force specific tool call with `tool_choice` `'<name_of_tool>'`:
+ ??? example "Force specific tool call with `tool_choice` `'<name_of_tool>'`"
- ```python
- from langchain_anthropic import ChatAnthropic
- from pydantic import BaseModel, Field
+ ```python
+ from langchain_anthropic import ChatAnthropic
+ from pydantic import BaseModel, Field
- class GetWeather(BaseModel):
- '''Get the current weather in a given location'''
+ class GetWeather(BaseModel):
+ '''Get the current weather in a given location'''
- location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
+ location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
- class GetPrice(BaseModel):
- '''Get the price of a specific product.'''
+ class GetPrice(BaseModel):
+ '''Get the price of a specific product.'''
- product: str = Field(..., description="The product to look up.")
+ product: str = Field(..., description="The product to look up.")
- model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
- model_with_tools = model.bind_tools([GetWeather, GetPrice], tool_choice="GetWeather")
- model_with_tools.invoke("What is the weather like in San Francisco")
- ```
+ model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
+ model_with_tools = model.bind_tools([GetWeather, GetPrice], tool_choice="GetWeather")
+ model_with_tools.invoke("What is the weather like in San Francisco")
+ ```
- Example — cache specific tools:
+ ??? example "Cache specific tools"
- ```python
- from langchain_anthropic import ChatAnthropic, convert_to_anthropic_tool
- from pydantic import BaseModel, Field
+ ```python
+ from langchain_anthropic import ChatAnthropic, convert_to_anthropic_tool
+ from pydantic import BaseModel, Field
- class GetWeather(BaseModel):
- '''Get the current weather in a given location'''
+ class GetWeather(BaseModel):
+ '''Get the current weather in a given location'''
- location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
+ location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
- class GetPrice(BaseModel):
- '''Get the price of a specific product.'''
+ class GetPrice(BaseModel):
+ '''Get the price of a specific product.'''
- product: str = Field(..., description="The product to look up.")
+ product: str = Field(..., description="The product to look up.")
- # We'll convert our pydantic class to the anthropic tool format
- # before passing to bind_tools so that we can set the 'cache_control'
- # field on our tool.
- cached_price_tool = convert_to_anthropic_tool(GetPrice)
- # Currently the only supported "cache_control" value is
- # {"type": "ephemeral"}.
- cached_price_tool["cache_control"] = {"type": "ephemeral"}
+ # We'll convert our pydantic class to the anthropic tool format
+ # before passing to bind_tools so that we can set the 'cache_control'
+ # field on our tool.
+ cached_price_tool = convert_to_anthropic_tool(GetPrice)
- # We need to pass in extra headers to enable use of the beta cache
- # control API.
- model = ChatAnthropic(
- model="claude-sonnet-4-5-20250929",
- temperature=0,
- )
- model_with_tools = model.bind_tools([GetWeather, cached_price_tool])
- model_with_tools.invoke("What is the weather like in San Francisco")
- ```
+ # Currently the only supported "cache_control" value is {"type": "ephemeral"}
+ cached_price_tool["cache_control"] = {"type": "ephemeral"}
- This outputs:
+ # Need to pass in extra headers to enable use of the beta cache control API.
+ model = ChatAnthropic(
+ model="claude-sonnet-4-5-20250929",
+ temperature=0,
+ )
+ model_with_tools = model.bind_tools([GetWeather, cached_price_tool])
+ model_with_tools.invoke("What is the weather like in San Francisco")
+ ```
- ```python
- AIMessage(
- content=[
- {
- "text": "Certainly! I can help you find out the current weather in San Francisco. To get this information, I'll use the GetWeather function. Let me fetch that data for you right away.",
- "type": "text",
- },
- {
- "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM",
- "input": {"location": "San Francisco, CA"},
- "name": "GetWeather",
- "type": "tool_use",
+ This outputs:
+
+ ```python
+ AIMessage(
+ content=[
+ {
+ "text": "Certainly! I can help you find out the current weather in San Francisco. To get this information, I'll use the GetWeather function. Let me fetch that data for you right away.",
+ "type": "text",
+ },
+ {
+ "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM",
+ "input": {"location": "San Francisco, CA"},
+ "name": "GetWeather",
+ "type": "tool_use",
+ },
+ ],
+ response_metadata={
+ "id": "msg_01Xg7Wr5inFWgBxE5jH9rpRo",
+ "model": "claude-sonnet-4-5-20250929",
+ "stop_reason": "tool_use",
+ "stop_sequence": None,
+ "usage": {
+ "input_tokens": 171,
+ "output_tokens": 96,
+ "cache_creation_input_tokens": 1470,
+ "cache_read_input_tokens": 0,
+ },
},
- ],
- response_metadata={
- "id": "msg_01Xg7Wr5inFWgBxE5jH9rpRo",
- "model": "claude-sonnet-4-5-20250929",
- "stop_reason": "tool_use",
- "stop_sequence": None,
- "usage": {
+ id="run-b36a5b54-5d69-470e-a1b0-b932d00b089e-0",
+ tool_calls=[
+ {
+ "name": "GetWeather",
+ "args": {"location": "San Francisco, CA"},
+ "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM",
+ "type": "tool_call",
+ }
+ ],
+ usage_metadata={
"input_tokens": 171,
"output_tokens": 96,
- "cache_creation_input_tokens": 1470,
- "cache_read_input_tokens": 0,
+ "total_tokens": 267,
},
- },
- id="run-b36a5b54-5d69-470e-a1b0-b932d00b089e-0",
- tool_calls=[
- {
- "name": "GetWeather",
- "args": {"location": "San Francisco, CA"},
- "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM",
- "type": "tool_call",
- }
- ],
- usage_metadata={
- "input_tokens": 171,
- "output_tokens": 96,
- "total_tokens": 267,
- },
- )
- ```
+ )
+ ```
- If we invoke the tool again, we can see that the "usage" information in the AIMessage.response_metadata shows that we had a cache hit:
+ If we invoke the tool again, we can see that the "usage" information in the `AIMessage.response_metadata` shows that we had a cache hit:
- ```python
- AIMessage(
- content=[
- {
- "text": "To get the current weather in San Francisco, I can use the GetWeather function. Let me check that for you.",
- "type": "text",
- },
- {
- "id": "toolu_01HtVtY1qhMFdPprx42qU2eA",
- "input": {"location": "San Francisco, CA"},
- "name": "GetWeather",
- "type": "tool_use",
+ ```python hl_lines="23"
+ AIMessage(
+ content=[
+ {
+ "text": "To get the current weather in San Francisco, I can use the GetWeather function. Let me check that for you.",
+ "type": "text",
+ },
+ {
+ "id": "toolu_01HtVtY1qhMFdPprx42qU2eA",
+ "input": {"location": "San Francisco, CA"},
+ "name": "GetWeather",
+ "type": "tool_use",
+ },
+ ],
+ response_metadata={
+ "id": "msg_016RfWHrRvW6DAGCdwB6Ac64",
+ "model": "claude-sonnet-4-5-20250929",
+ "stop_reason": "tool_use",
+ "stop_sequence": None,
+ "usage": {
+ "input_tokens": 171,
+ "output_tokens": 82,
+ "cache_creation_input_tokens": 0,
+ "cache_read_input_tokens": 1470,
+ },
},
- ],
- response_metadata={
- "id": "msg_016RfWHrRvW6DAGCdwB6Ac64",
- "model": "claude-sonnet-4-5-20250929",
- "stop_reason": "tool_use",
- "stop_sequence": None,
- "usage": {
+ id="run-88b1f825-dcb7-4277-ac27-53df55d22001-0",
+ tool_calls=[
+ {
+ "name": "GetWeather",
+ "args": {"location": "San Francisco, CA"},
+ "id": "toolu_01HtVtY1qhMFdPprx42qU2eA",
+ "type": "tool_call",
+ }
+ ],
+ usage_metadata={
"input_tokens": 171,
"output_tokens": 82,
- "cache_creation_input_tokens": 0,
- "cache_read_input_tokens": 1470,
+ "total_tokens": 253,
},
- },
- id="run-88b1f825-dcb7-4277-ac27-53df55d22001-0",
- tool_calls=[
- {
- "name": "GetWeather",
- "args": {"location": "San Francisco, CA"},
- "id": "toolu_01HtVtY1qhMFdPprx42qU2eA",
- "type": "tool_call",
- }
- ],
- usage_metadata={
- "input_tokens": 171,
- "output_tokens": 82,
- "total_tokens": 253,
- },
- )
- ```
+ )
+ ```
+
+ ??? example "Strict tool use"
+
+ Strict tool use guarantees that tool names and arguments are validated
+ and correctly typed.
+
+ !!! note
+
+ Strict tool use requires:
+
+ - Claude Sonnet 4.5 or Opus 4.1
+ - `langchain-anthropic>=1.1.0`
+
+ To enable strict tool use:
+
+ 1. Specify the `structured-outputs-2025-11-13` beta header
+ 2. Specify `strict=True` when calling `bind_tools`
+
+ ```python hl_lines="5 12"
+ from langchain_anthropic import ChatAnthropic
+
+ model = ChatAnthropic(
+ model="claude-sonnet-4-5",
+ betas=["structured-outputs-2025-11-13"],
+ )
+
+ 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)
+ ```
+
+ See LangChain [docs](https://docs.langchain.com/oss/python/integrations/chat/anthropic#strict-tool-use)
+ for more detail.
""" # noqa: E501
formatted_tools = [
tool
@@ -2277,19 +2403,20 @@ class ChatAnthropic(BaseChatModel):
method: The structured output method to use. Options are:
- `'function_calling'` (default): Use forced tool calling to get
- structured output.
+ structured output.
- `'json_schema'`: Use Claude's dedicated
- [structured output](https://docs.claude.com/en/docs/build-with-claude/structured-outputs)
- feature.
+ [structured output](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
+ feature.
kwargs: Additional keyword arguments are ignored.
Returns:
A `Runnable` that takes same inputs as a
- `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is
- `False` and `schema` is a Pydantic class, `Runnable` outputs an instance
- of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is
- `False` then `Runnable` outputs a `dict`.
+ `langchain_core.language_models.chat.BaseChatModel`.
+
+ If `include_raw` is `False` and `schema` is a Pydantic class, `Runnable`
+ outputs an instance of `schema` (i.e., a Pydantic object). Otherwise, if
+ `include_raw` is `False` then `Runnable` outputs a `dict`.
If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:
@@ -2298,82 +2425,120 @@ class ChatAnthropic(BaseChatModel):
depends on the `schema` as described above.
- `'parsing_error'`: `BaseException | None`
- Example: Pydantic schema (`include_raw=False`):
+ ??? example "Pydantic schema (`include_raw=False`)"
- ```python
- from langchain_anthropic import ChatAnthropic
- from pydantic import BaseModel
+ ```python
+ from langchain_anthropic import ChatAnthropic
+ from pydantic import BaseModel
- class AnswerWithJustification(BaseModel):
- '''An answer to the user question along with justification for the answer.'''
+ class AnswerWithJustification(BaseModel):
+ '''An answer to the user question along with justification for the answer.'''
- answer: str
- justification: str
+ answer: str
+ justification: str
- model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
- structured_model = model.with_structured_output(AnswerWithJustification)
+ model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
+ structured_model = model.with_structured_output(AnswerWithJustification)
- structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
+ structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
+ # -> AnswerWithJustification(
+ # answer='They weigh the same',
+ # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
+ # )
+ ```
- # -> AnswerWithJustification(
- # answer='They weigh the same',
- # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
- # )
- ```
+ ??? example "Pydantic schema (`include_raw=True`)"
- Example: Pydantic schema (`include_raw=True`):
+ ```python
+ from langchain_anthropic import ChatAnthropic
+ from pydantic import BaseModel
- ```python
- from langchain_anthropic import ChatAnthropic
- from pydantic import BaseModel
+ class AnswerWithJustification(BaseModel):
+ '''An answer to the user question along with justification for the answer.'''
- class AnswerWithJustification(BaseModel):
- '''An answer to the user question along with justification for the answer.'''
+ answer: str
+ justification: str
- answer: str
- justification: str
+ model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
+ structured_model = model.with_structured_output(AnswerWithJustification, include_raw=True)
+
+ structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
+ # -> {
+ # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
+ # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
+ # 'parsing_error': None
+ # }
+ ```
- model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
- structured_model = model.with_structured_output(AnswerWithJustification, include_raw=True)
+ ??? example "Dictionary schema (`include_raw=False`)"
- structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
- # -> {
- # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
- # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
- # 'parsing_error': None
- # }
- ```
+ ```python
+ from langchain_anthropic import ChatAnthropic
- Example: `dict` schema (`include_raw=False`):
+ schema = {
+ "name": "AnswerWithJustification",
+ "description": "An answer to the user question along with justification for the answer.",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "answer": {"type": "string"},
+ "justification": {"type": "string"},
+ },
+ "required": ["answer", "justification"],
+ },
+ }
+ model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
+ structured_model = model.with_structured_output(schema)
- ```python
- from langchain_anthropic import ChatAnthropic
+ structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
+ # -> {
+ # 'answer': 'They weigh the same',
+ # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
+ # }
+ ```
- schema = {
- "name": "AnswerWithJustification",
- "description": "An answer to the user question along with justification for the answer.",
- "input_schema": {
- "type": "object",
- "properties": {
- "answer": {"type": "string"},
- "justification": {"type": "string"},
- },
- "required": ["answer", "justification"],
- },
- }
- model = ChatAnthropic(model="claude-sonnet-4-5-20250929", temperature=0)
- structured_model = model.with_structured_output(schema)
-
- structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
- # -> {
- # 'answer': 'They weigh the same',
- # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
- # }
- ```
+ ??? example "Native structured output with `method='json_schema'`"
+
+ Anthropic supports a native structured output feature that guarantees
+ responses adhere to a given schema.
+
+ !!! note
+
+ Native structured output requires:
+
+ - Claude Sonnet 4.5 or Opus 4.1
+ - `langchain-anthropic>=1.1.0`
+
+ To enable native structured output:
+
+ 1. Specify the `structured-outputs-2025-11-13` beta header
+ 2. Specify `method="json_schema"` when calling `with_structured_output`
+
+ ```python hl_lines="6 16"
+ from langchain_anthropic import ChatAnthropic
+ from pydantic import BaseModel, Field
+
+ model = ChatAnthropic(
+ model="claude-sonnet-4-5",
+ betas=["structured-outputs-2025-11-13"],
+ )
+
+ 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")
+ print(response)
+ # -> Movie(title="Inception", year=2010, director="Christopher Nolan", rating=8.8)
+ ```
""" # noqa: E501
if method == "json_mode":
warning_message = (
@@ -2458,56 +2623,56 @@ class ChatAnthropic(BaseChatModel):
kwargs: Additional keyword arguments are passed to the Anthropic
`messages.count_tokens` method.
- Basic usage:
+ ???+ example "Basic usage"
- ```python
- from langchain_anthropic import ChatAnthropic
- from langchain_core.messages import HumanMessage, SystemMessage
+ ```python
+ from langchain_anthropic import ChatAnthropic
+ from langchain_core.messages import HumanMessage, SystemMessage
- model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
+ model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
- messages = [
- SystemMessage(content="You are a scientist"),
- HumanMessage(content="Hello, Claude"),
- ]
- model.get_num_tokens_from_messages(messages)
- ```
+ messages = [
+ SystemMessage(content="You are a scientist"),
+ HumanMessage(content="Hello, Claude"),
+ ]
+ model.get_num_tokens_from_messages(messages)
+ ```
- ```txt
- 14
- ```
+ ```txt
+ 14
+ ```
- Pass tool schemas:
+ ??? example "Pass tool schemas"
- ```python
- from langchain_anthropic import ChatAnthropic
- from langchain_core.messages import HumanMessage
- from langchain_core.tools import tool
+ ```python
+ from langchain_anthropic import ChatAnthropic
+ from langchain_core.messages import HumanMessage
+ from langchain_core.tools import tool
- model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
+ model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
- @tool(parse_docstring=True)
- def get_weather(location: str) -> str:
- \"\"\"Get the current weather in a given location
+ @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"
+ Args:
+ location: The city and state, e.g. San Francisco, CA
+ \"\"\"
+ return "Sunny"
- messages = [
- HumanMessage(content="What's the weather like in San Francisco?"),
- ]
- model.get_num_tokens_from_messages(messages, tools=[get_weather])
- ```
+ messages = [
+ HumanMessage(content="What's the weather like in San Francisco?"),
+ ]
+ model.get_num_tokens_from_messages(messages, tools=[get_weather])
+ ```
- ```txt
- 403
- ```
+ ```txt
+ 403
+ ```
!!! warning "Behavior changed in `langchain-anthropic` 0.3.0"
- Uses Anthropic's [token counting API](https://docs.claude.com/en/docs/build-with-claude/token-counting) to count tokens in messages.
+ Uses Anthropic's [token counting API](https://platform.claude.com/docs/en/build-with-claude/token-counting) to count tokens in messages.
""" # noqa: D214,E501
formatted_system, formatted_messages = _format_messages(messages)
@@ -2539,7 +2704,21 @@ def convert_to_anthropic_tool(
*,
strict: bool | None = None,
) -> AnthropicTool:
- """Convert a tool-like object to an Anthropic tool definition."""
+ """Convert a tool-like object to an Anthropic tool definition.
+
+ Args:
+ tool: A tool-like object to convert. Can be an Anthropic tool dict,
+ a Pydantic model, a function, or a `BaseTool`.
+ strict: If `True`, enables strict schema adherence for the tool.
+
+ !!! note
+
+ Requires Claude Sonnet 4.5 or Opus 4.1 and the
+ `structured-outputs-2025-11-13` beta header.
+
+ Returns:
+ An Anthropic tool definition dict.
+ """
# already in Anthropic tool format
if isinstance(tool, dict) and all(
k in tool for k in ("name", "description", "input_schema")
@@ -2605,9 +2784,9 @@ def _lc_tool_calls_to_anthropic_tool_use_blocks(
def _convert_to_anthropic_output_format(schema: dict | type) -> dict[str, Any]:
- """Convert JSON schema, Pydantic model, or TypedDict into Claude output_format.
+ """Convert JSON schema, Pydantic model, or `TypedDict` into Claude `output_format`.
- See: https://docs.claude.com/en/docs/build-with-claude/structured-outputs
+ See Claude docs on [structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs).
"""
from anthropic import transform_schema
@@ -2633,23 +2812,24 @@ def _make_message_chunk_from_anthropic_event(
event: Raw streaming event from Anthropic SDK
stream_usage: Whether to include usage metadata in the output chunks.
coerce_content_to_string: Whether to convert structured content to plain
- text strings. When True, only text content is preserved; when False,
- structured content like tool calls and citations are maintained.
+ text strings.
+
+ When `True`, only text content is preserved; when `False`, structured
+ content like tool calls and citations are maintained.
block_start_event: Previous content block start event, used for tracking
tool use blocks and maintaining context across related events.
Returns:
- Tuple containing:
- - AIMessageChunk: Converted message chunk with appropriate content and
- metadata, or None if the event doesn't produce a chunk
- - RawMessageStreamEvent: Updated `block_start_event` for tracking content
- blocks across sequential events, or None if not applicable
+ Tuple with
+ - `AIMessageChunk`: Converted message chunk with appropriate content and
+ metadata, or `None` if the event doesn't produce a chunk
+ - `RawMessageStreamEvent`: Updated `block_start_event` for tracking content
+ blocks across sequential events, or `None` if not applicable
Note:
Not all Anthropic events result in message chunks. Events like internal
- state changes return None for the message chunk while potentially
+ state changes return `None` for the message chunk while potentially
updating the `block_start_event` for context tracking.
-
"""
message_chunk: AIMessageChunk | None = None
# Reference: Anthropic SDK streaming implementation
@@ -2768,7 +2948,7 @@ def _make_message_chunk_from_anthropic_event(
# Mark final Anthropic stream chunk
message_chunk.chunk_position = "last"
# Unhandled event types (e.g., `content_block_stop`, `ping` events)
- # https://docs.claude.com/en/docs/build-with-claude/streaming#other-events
+ # https://platform.claude.com/docs/en/build-with-claude/streaming#other-events
else:
pass
@@ -2780,9 +2960,9 @@ def _make_message_chunk_from_anthropic_event(
def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata:
"""Create LangChain `UsageMetadata` from Anthropic `Usage` data.
- Note: Anthropic's `input_tokens` excludes cached tokens, so we manually add
- `cache_read` and `cache_creation` tokens to get the true total.
-
+ Note:
+ Anthropic's `input_tokens` excludes cached tokens, so we manually add
+ `cache_read` and `cache_creation` tokens to get the true total.
"""
input_token_details: dict = {
"cache_read": getattr(anthropic_usage, "cache_read_input_tokens", None),
diff --git a/libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py b/libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py
index 5641f76895..ffc491a1fe 100644
--- a/libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py
+++ b/libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py
@@ -64,10 +64,10 @@ def files_reducer(
Args:
left: Existing files dict.
- right: New files dict to merge (None values delete files).
+ right: New files dict to merge (`None` values delete files).
Returns:
- Merged dict where right overwrites left for matching keys.
+ Merged `dict` where right overwrites left for matching keys.
"""
if left is None:
# Filter out None values when initializing
@@ -135,7 +135,7 @@ def _list_directory(files: dict[str, FileData], path: str) -> list[str]:
"""List files in a directory.
Args:
- files: Files dict.
+ files: Files `dict`.
path: Normalized directory path.
Returns:
@@ -209,7 +209,7 @@ class _StateClaudeFileToolMiddleware(AgentMiddleware):
new_str: Replacement string for str_replace command.
insert_line: Line number for insert command.
new_path: New path for rename command.
- view_range: Line range [start, end] for view command.
+ view_range: Line range `[start, end]` for view command.
Returns:
Command for state update or string result.
@@ -572,7 +572,7 @@ class _StateClaudeFileToolMiddleware(AgentMiddleware):
class StateClaudeTextEditorMiddleware(_StateClaudeFileToolMiddleware):
"""State-based text editor tool middleware.
- Provides Anthropic's text_editor tool using LangGraph state for storage.
+ Provides Anthropic's `text_editor` tool using LangGraph state for storage.
Files persist for the conversation thread.
Example:
@@ -597,6 +597,7 @@ class StateClaudeTextEditorMiddleware(_StateClaudeFileToolMiddleware):
Args:
allowed_path_prefixes: Optional list of allowed path prefixes.
+
If specified, only paths starting with these prefixes are allowed.
"""
super().__init__(
@@ -611,8 +612,9 @@ class StateClaudeMemoryMiddleware(_StateClaudeFileToolMiddleware):
"""State-based memory tool middleware.
Provides Anthropic's memory tool using LangGraph state for storage.
- Files persist for the conversation thread. Enforces /memories prefix
- and injects Anthropic's recommended system prompt.
+ Files persist for the conversation thread.
+
+ Enforces `/memories` prefix and injects Anthropic's recommended system prompt.
Example:
```python
@@ -637,9 +639,11 @@ class StateClaudeMemoryMiddleware(_StateClaudeFileToolMiddleware):
Args:
allowed_path_prefixes: Optional list of allowed path prefixes.
- Defaults to ["/memories"].
- system_prompt: System prompt to inject. Defaults to Anthropic's
- recommended memory prompt.
+
+ Defaults to `['/memories']`.
+ system_prompt: System prompt to inject.
+
+ Defaults to Anthropic's recommended memory prompt.
"""
super().__init__(
tool_type=MEMORY_TOOL_TYPE,
@@ -699,15 +703,15 @@ class _FilesystemClaudeFileToolMiddleware(AgentMiddleware):
"""Execute file operations on filesystem.
Args:
- runtime: Tool runtime providing tool_call_id.
+ runtime: Tool runtime providing `tool_call_id`.
command: Operation to perform.
path: File path to operate on.
file_text: Full file content for create command.
- old_str: String to replace for str_replace command.
- new_str: Replacement string for str_replace command.
+ old_str: String to replace for `str_replace` command.
+ new_str: Replacement string for `str_replace` command.
insert_line: Line number for insert command.
new_path: New path for rename command.
- view_range: Line range [start, end] for view command.
+ view_range: Line range `[start, end]` for view command.
Returns:
Command for message update or string result.
@@ -811,14 +815,14 @@ class _FilesystemClaudeFileToolMiddleware(AgentMiddleware):
"""Validate and resolve a virtual path to filesystem path.
Args:
- path: Virtual path (e.g., /file.txt or /src/main.py).
+ path: Virtual path (e.g., `/file.txt` or `/src/main.py`).
Returns:
- Resolved absolute filesystem path within root_path.
+ Resolved absolute filesystem path within `root_path`.
Raises:
ValueError: If path contains traversal attempts, escapes root directory,
- or violates allowed_prefixes restrictions.
+ or violates `allowed_prefixes` restrictions.
"""
# Normalize path
if not path.startswith("/"):
@@ -922,7 +926,7 @@ class _FilesystemClaudeFileToolMiddleware(AgentMiddleware):
)
def _handle_str_replace(self, args: dict, tool_call_id: str | None) -> Command:
- """Handle str_replace command."""
+ """Handle `str_replace` command."""
path = args["path"]
old_str = args["old_str"]
new_str = args.get("new_str", "")
@@ -1060,7 +1064,7 @@ class _FilesystemClaudeFileToolMiddleware(AgentMiddleware):
class FilesystemClaudeTextEditorMiddleware(_FilesystemClaudeFileToolMiddleware):
"""Filesystem-based text editor tool middleware.
- Provides Anthropic's text_editor tool using local filesystem for storage.
+ Provides Anthropic's `text_editor` tool using local filesystem for storage.
User handles persistence via volumes, git, or other mechanisms.
Example:
@@ -1087,9 +1091,12 @@ class FilesystemClaudeTextEditorMiddleware(_FilesystemClaudeFileToolMiddleware):
Args:
root_path: Root directory for file operations.
- allowed_prefixes: Optional list of allowed virtual path prefixes
- (default: ["/"]).
- max_file_size_mb: Maximum file size in MB (default: 10).
+ allowed_prefixes: Optional list of allowed virtual path prefixes.
+
+ Defaults to `['/']`.
+ max_file_size_mb: Maximum file size in MB
+
+ Defaults to `10`.
"""
super().__init__(
tool_type=TEXT_EDITOR_TOOL_TYPE,
@@ -1105,7 +1112,9 @@ class FilesystemClaudeMemoryMiddleware(_FilesystemClaudeFileToolMiddleware):
Provides Anthropic's memory tool using local filesystem for storage.
User handles persistence via volumes, git, or other mechanisms.
- Enforces /memories prefix and injects Anthropic's recommended system prompt.
+
+ Enforces `/memories` prefix and injects Anthropic's recommended system
+ prompt.
Example:
```python
@@ -1133,10 +1142,14 @@ class FilesystemClaudeMemoryMiddleware(_FilesystemClaudeFileToolMiddleware):
Args:
root_path: Root directory for file operations.
allowed_prefixes: Optional list of allowed virtual path prefixes.
- Defaults to ["/memories"].
- max_file_size_mb: Maximum file size in MB (default: 10).
- system_prompt: System prompt to inject. Defaults to Anthropic's
- recommended memory prompt.
+
+ Defaults to `['/memories']`.
+ max_file_size_mb: Maximum file size in MB
+
+ Defaults to `10`.
+ system_prompt: System prompt to inject.
+
+ Defaults to Anthropic's recommended memory prompt.
"""
super().__init__(
tool_type=MEMORY_TOOL_TYPE,
diff --git a/libs/partners/anthropic/langchain_anthropic/middleware/bash.py b/libs/partners/anthropic/langchain_anthropic/middleware/bash.py
index dd49ae4a77..6348ad0381 100644
--- a/libs/partners/anthropic/langchain_anthropic/middleware/bash.py
+++ b/libs/partners/anthropic/langchain_anthropic/middleware/bash.py
@@ -34,6 +34,7 @@ class ClaudeBashToolMiddleware(ShellToolMiddleware):
Args:
workspace_root: Base directory for the shell session.
+
If omitted, a temporary directory is created.
startup_commands: Optional commands executed after the session starts.
shutdown_commands: Optional commands executed before session shutdown.
diff --git a/libs/partners/anthropic/langchain_anthropic/middleware/file_search.py b/libs/partners/anthropic/langchain_anthropic/middleware/file_search.py
index a60122e40b..0f4fd42c56 100644
--- a/libs/partners/anthropic/langchain_anthropic/middleware/file_search.py
+++ b/libs/partners/anthropic/langchain_anthropic/middleware/file_search.py
@@ -21,7 +21,7 @@ from langchain_anthropic.middleware.anthropic_tools import AnthropicToolsState
def _expand_include_patterns(pattern: str) -> list[str] | None:
- """Expand brace patterns like ``*.{py,pyi}`` into a list of globs."""
+ """Expand brace patterns like `*.{py,pyi}` into a list of globs."""
if "}" in pattern and "{" not in pattern:
return None
@@ -76,7 +76,7 @@ def _is_valid_include_pattern(pattern: str) -> bool:
def _match_include_pattern(basename: str, pattern: str) -> bool:
- """Return True if the basename matches the include pattern."""
+ """Return `True` if the basename matches the include pattern."""
expanded = _expand_include_patterns(pattern)
if not expanded:
return False
@@ -88,6 +88,7 @@ class StateFileSearchMiddleware(AgentMiddleware):
"""Provides Glob and Grep search over state-based files.
This middleware adds two tools that search through virtual files in state:
+
- Glob: Fast file pattern matching by file path
- Grep: Fast content search using regular expressions
@@ -120,8 +121,9 @@ class StateFileSearchMiddleware(AgentMiddleware):
"""Initialize the search middleware.
Args:
- state_key: State key to search (default: "text_editor_files").
- Use "memory_files" to search memory tool files.
+ state_key: State key to search
+
+ Use `'memory_files'` to search memory tool files.
"""
self.state_key = state_key
@@ -134,18 +136,23 @@ class StateFileSearchMiddleware(AgentMiddleware):
) -> str:
"""Fast file pattern matching tool that works with any codebase size.
- Supports glob patterns like **/*.js or src/**/*.ts.
+ Supports glob patterns like `**/*.js` or `src/**/*.ts`.
+
Returns matching file paths sorted by modification time.
+
Use this tool when you need to find files by name patterns.
Args:
pattern: The glob pattern to match files against.
- path: The directory to search in. If not specified, searches from root.
+ path: The directory to search in.
+
+ If not specified, searches from root.
Returns:
Newline-separated list of matching file paths, sorted by modification
- time (most recently modified first). Returns "No files found" if no
- matches.
+ time (most recently modified first).
+
+ Returns `'No files found'` if no matches.
"""
return self._handle_glob_search(pattern, path, runtime.state)
@@ -161,21 +168,27 @@ class StateFileSearchMiddleware(AgentMiddleware):
) -> str:
"""Fast content search tool that works with any codebase size.
- Searches file contents using regular expressions. Supports full regex
- syntax and filters files by pattern with the include parameter.
+ Searches file contents using regular expressions.
+
+ Supports full regex syntax and filters files by pattern with the include
+ parameter.
Args:
pattern: The regular expression pattern to search for in file contents.
path: The directory to search in. If not specified, searches from root.
- include: File pattern to filter (e.g., "*.js", "*.{ts,tsx}").
- output_mode: Output format:
- - "files_with_matches": Only file paths containing matches (default)
- - "content": Matching lines with file:line:content format
- - "count": Count of matches per file
+ include: File pattern to filter (e.g., `'*.js'`, `'*.{ts,tsx}'`).
+ output_mode: Output format.
+
+ Options:
+
+ - `'files_with_matches'`: Only file paths containing matches
+ - `'content'`: Matching lines with file:line:content format
+ - `'count'`: Count of matches per file
Returns:
- Search results formatted according to output_mode. Returns "No matches
- found" if no results.
+ Search results formatted according to `output_mode`.
+
+ Returns `'No matches found'` if no results.
"""
return self._handle_grep_search(
pattern, path, include, output_mode, runtime.state
@@ -200,8 +213,9 @@ class StateFileSearchMiddleware(AgentMiddleware):
Returns:
Newline-separated list of matching file paths, sorted by modification
- time (most recently modified first). Returns "No files found" if no
- matches.
+ time (most recently modified first).
+
+ Returns `'No files found'` if no matches.
"""
# Normalize base path
base_path = path if path.startswith("/") else "/" + path
@@ -257,13 +271,14 @@ class StateFileSearchMiddleware(AgentMiddleware):
Args:
pattern: The regular expression pattern to search for in file contents.
path: The directory to search in.
- include: File pattern to filter (e.g., "*.js", "*.{ts,tsx}").
+ include: File pattern to filter (e.g., `'*.js'`, `'*.{ts,tsx}'`).
output_mode: Output format.
state: The current agent state.
Returns:
- Search results formatted according to output_mode. Returns "No matches
- found" if no results.
+ Search results formatted according to `output_mode`.
+
+ Returns `'No matches found'` if no results.
"""
# Normalize base path
base_path = path if path.startswith("/") else "/" + path
diff --git a/libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py b/libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py
index eb37d17837..f27285c7f0 100644
--- a/libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py
+++ b/libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py
@@ -1,8 +1,8 @@
"""Anthropic prompt caching middleware.
Requires:
- - langchain: For agent middleware framework
- - langchain-anthropic: For ChatAnthropic model (already a dependency)
+ - `langchain`: For agent middleware framework
+ - `langchain-anthropic`: For `ChatAnthropic` model (already a dependency)
"""
from collections.abc import Awaitable, Callable
@@ -32,10 +32,10 @@ class AnthropicPromptCachingMiddleware(AgentMiddleware):
Optimizes API usage by caching conversation prefixes for Anthropic models.
- Requires both 'langchain' and 'langchain-anthropic' packages to be installed.
+ Requires both `langchain` and `langchain-anthropic` packages to be installed.
Learn more about Anthropic prompt caching
- [here](https://docs.claude.com/en/docs/build-with-claude/prompt-caching).
+ [here](https://platform.claude.com/docs/en/build-with-claude/prompt-caching).
"""
def __init__(
@@ -48,16 +48,20 @@ class AnthropicPromptCachingMiddleware(AgentMiddleware):
"""Initialize the middleware with cache control settings.
Args:
- type: The type of cache to use, only "ephemeral" is supported.
- ttl: The time to live for the cache, only "5m" and "1h" are
+ type: The type of cache to use, only `'ephemeral'` is supported.
+ ttl: The time to live for the cache, only `'5m'` and `'1h'` are
supported.
min_messages_to_cache: The minimum number of messages until the
- cache is used, default is 0.
+ cache is used.
unsupported_model_behavior: The behavior to take when an
- unsupported model is used. "ignore" will ignore the unsupported
- model and continue without caching. "warn" will warn the user
- and continue without caching. "raise" will raise an error and
- stop the agent.
+ unsupported model is used.
+
+ `'ignore'` will ignore the unsupported model and continue without
+ caching.
+
+ `'warn'` will warn the user and continue without caching.
+
+ `'raise'` will raise an error and stop the agent.
"""
self.type = type
self.ttl = ttl
@@ -71,10 +75,10 @@ class AnthropicPromptCachingMiddleware(AgentMiddleware):
request: The model request to check.
Returns:
- True if caching should be applied, False otherwise.
+ `True` if caching should be applied, `False` otherwise.
Raises:
- ValueError: If model is unsupported and behavior is set to "raise".
+ ValueError: If model is unsupported and behavior is set to `'raise'`.
"""
if not isinstance(request.model, ChatAnthropic):
msg = (
diff --git a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py b/libs/partners/anthropic/tests/integration_tests/test_chat_models.py
index 0201233dfd..5bb043eb9c 100644
--- a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py
+++ b/libs/partners/anthropic/tests/integration_tests/test_chat_models.py
@@ -536,7 +536,7 @@ def test_tool_use() -> None:
assert content_blocks[1]["args"]
# Testing token-efficient tools
- # https://docs.claude.com/en/docs/agents-and-tools/tool-use/token-efficient-tool-use
+ # https://platform.claude.com/docs/en/agents-and-tools/tool-use/token-efficient-tool-use
assert gathered.usage_metadata
assert response.usage_metadata
assert (
@@ -1598,7 +1598,7 @@ def test_code_execution_old(output_version: Literal["v0", "v1"]) -> None:
See the `test_code_execution` test below to test the current
`code_execution_20250825` tool.
- Migration guide: https://docs.claude.com/en/docs/agents-and-tools/tool-use/code-execution-tool#upgrade-to-latest-tool-version
+ Migration guide: https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool#upgrade-to-latest-tool-version
"""
llm = ChatAnthropic(
model=MODEL_NAME, # type: ignore[call-arg]