Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Environment:Langchain ai Langchain Anthropic API Credentials

From Leeroopedia
Knowledge Sources
Domains Infrastructure, LLMs
Last Updated 2026-02-11 14:00 GMT

Overview

Environment credentials and SDK dependencies required for Anthropic Claude API integration via `langchain-anthropic`.

Description

The `langchain-anthropic` package provides the `ChatAnthropic` class for interacting with Claude models. It requires an Anthropic API key and depends on `anthropic >= 0.78.0`. The package supports tool calling, structured output, streaming, and Anthropic-specific features like prompt caching and built-in tools (web fetch, code execution, computer use).

Usage

Required for any workflow that uses Anthropic Claude models. This includes chat completions, tool calling, structured output, and streaming with Claude. Some features require specific beta headers managed via `_TOOL_TYPE_TO_BETA` mapping.

System Requirements

Category Requirement Notes
Python >= 3.10.0, < 4.0.0 Same as core
Network HTTPS outbound to api.anthropic.com Or custom base URL
SDK anthropic >= 0.78.0, < 1.0.0 Official Anthropic Python SDK

Dependencies

Python Packages

  • `langchain-core` >= 1.2.11, < 2.0.0
  • `anthropic` >= 0.78.0, < 1.0.0
  • `pydantic` >= 2.7.4, < 3.0.0

Credentials

The following environment variables must be set:

Required:

  • `ANTHROPIC_API_KEY`: Anthropic API key for authentication.

Optional:

  • `ANTHROPIC_API_URL` or `ANTHROPIC_BASE_URL`: Custom API base URL (default: `https://api.anthropic.com`).
  • `ANTHROPIC_PROXY`: Proxy URL for Anthropic connections.

Integration Testing Only:

  • `ANTHROPIC_FILES_API_IMAGE_ID`: Image file ID for file search tests.
  • `ANTHROPIC_FILES_API_PDF_ID`: PDF file ID for file search tests.

Quick Install

pip install langchain-anthropic>=1.3.3

# Set API key
export ANTHROPIC_API_KEY="sk-ant-..."

Code Evidence

Fallback max output tokens from `libs/partners/anthropic/langchain_anthropic/chat_models.py:93`:

_FALLBACK_MAX_OUTPUT_TOKENS: Final[int] = 4096

Beta header mapping for special tools from `libs/partners/anthropic/langchain_anthropic/chat_models.py:134-148`:

_TOOL_TYPE_TO_BETA: dict[str, str] = {
    "web_fetch_20250910": "web-fetch-2025-09-10",
    "code_execution_20250522": "code-execution-2025-05-22",
    "mcp_toolset": "mcp-client-2025-11-20",
    "memory_20250818": "context-management-2025-06-27",
    "computer_20250124": "computer-use-2025-01-24",
}

Client URL detection from `libs/partners/anthropic/langchain_anthropic/_client_utils.py:57-76`:

base_url = os.environ.get("ANTHROPIC_BASE_URL", "https://api.anthropic.com")

Common Errors

Error Message Cause Solution
`ValueError: Did not find anthropic_api_key` ANTHROPIC_API_KEY not set `export ANTHROPIC_API_KEY="sk-ant-..."` or pass `api_key=` parameter
`anthropic.AuthenticationError: 401` Invalid API key Verify API key at console.anthropic.com
`anthropic.BadRequestError: max_tokens required` max_tokens not set and model profile missing Set `max_tokens` explicitly or ensure model profiles are up to date

Compatibility Notes

  • Beta Features: Some tool types (web fetch, code execution, MCP, computer use) require versioned beta headers automatically injected by the SDK.
  • Fallback Tokens: When model profile is unavailable, `max_output_tokens` defaults to 4096.
  • Proxy Support: The `ANTHROPIC_PROXY` env var configures httpx proxy for corporate network access.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment