Nous.Providers.Anthropic (nous v0.16.2)
View SourceAnthropic Claude provider implementation.
Supports Claude models via the Anthropic Messages API.
Usage
# Basic usage
{:ok, response} = Nous.Providers.Anthropic.chat(%{
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [%{"role" => "user", "content" => "Hello"}]
})
# With system prompt
{:ok, response} = Nous.Providers.Anthropic.chat(%{
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
system: "You are a helpful assistant.",
messages: [%{"role" => "user", "content" => "Hello"}]
})
# With long context beta
{:ok, response} = Nous.Providers.Anthropic.chat(
params,
enable_long_context: true
)
# Streaming
{:ok, stream} = Nous.Providers.Anthropic.chat_stream(params)
Enum.each(stream, fn event -> IO.inspect(event) end)Configuration
# In config.exs
config :nous, :anthropic,
api_key: "sk-ant-...",
base_url: "https://api.anthropic.com" # optional
Summary
Functions
Get the API key from options, environment, or application config.
Get the base URL from options, application config, or default.
Count tokens in messages (rough estimate).
High-level request with message conversion, telemetry, and error wrapping.
High-level streaming request with message conversion and telemetry.
Functions
Get the API key from options, environment, or application config.
Lookup order:
:api_keyoption passed directly- Environment variable (ANTHROPIC_API_KEY)
- Application config:
config :nous, anthropic, api_key: "..."
Get the base URL from options, application config, or default.
Lookup order:
:base_urloption passed directly- Application config:
config :nous, anthropic, base_url: "..." - Default: https://api.anthropic.com
Count tokens in messages (rough estimate).
Override this in your provider for more accurate counting.
High-level request with message conversion, telemetry, and error wrapping.
Default implementation that:
- Converts messages to provider format
- Builds request params
- Calls chat/2
- Parses response
- Emits telemetry events
- Wraps errors
High-level streaming request with message conversion and telemetry.