The anthropic provider supports all APIs that use the same interface for the /v1/messages endpoint.

Example:

BAML
1client<llm> MyClient {
2 provider anthropic
3 options {
4 model "claude-3-5-sonnet-20240620"
5 temperature 0
6 }
7}

The options are passed through directly to the API, barring a few. Here’s a shorthand of the options:

Non-forwarded options

api_key
string

Will be passed as a bearer token. Default: env.ANTHROPIC_API_KEY

Authorization: Bearer $api_key

base_url
string

The base URL for the API. Default: https://api.anthropic.com

default_role
string

The default role for any prompts that don’t specify a role. Default: system

We don’t have any checks for this field, you can pass any string you wish.

headers
object

Additional headers to send with the request.

Unless specified with a different value, we inject in the following headers:

"anthropic-version" "2023-06-01"

Example:

1client<llm> MyClient {
2 provider anthropic
3 options {
4 api_key env.MY_ANTHROPIC_KEY
5 model "claude-3-5-sonnet-20240620"
6 headers {
7 "X-My-Header" "my-value"
8 }
9 }
10}
default_role
string

The role to use if the role is not in the allowed_roles. Default: "user" usually, but some models like OpenAI’s gpt-4o will use "system"

Picked the first role in allowed_roles if not “user”, otherwise “user”.

allowed_roles
string[]

Which roles should we forward to the API? Default: ["system", "user", "assistant"] usually, but some models like OpenAI’s o1-mini will use ["user", "assistant"]

When building prompts, any role not in this list will be set to the default_role.

allowed_role_metadata
string[]

Which role metadata should we forward to the API? Default: []

For example you can set this to ["cache_control"] to forward the cache policy to the API.

If you do not set allowed_role_metadata, we will not forward any role metadata to the API even if it is set in the prompt.

Then in your prompt you can use something like:

1client<llm> ClaudeWithCaching {
2 provider anthropic
3 options {
4 model claude-3-haiku-20240307
5 api_key env.ANTHROPIC_API_KEY
6 max_tokens 1000
7 allowed_role_metadata ["cache_control"]
8 headers {
9 "anthropic-beta" "prompt-caching-2024-07-31"
10 }
11 }
12}
13
14client<llm> FooWithout {
15 provider anthropic
16 options {
17 }
18}
19
20template_string Foo() #"
21 {{ _.role('user', cache_control={"type": "ephemeral"}) }}
22 This will be cached for ClaudeWithCaching, but not for FooWithout!
23 {{ _.role('user') }}
24 This will not be cached for Foo or FooWithout!
25"#

You can use the playground to see the raw curl request to see what is being sent to the API.

supports_streaming
boolean

Whether the internal LLM client should use the streaming API. Default: true

Then in your prompt you can use something like:

1client<llm> MyClientWithoutStreaming {
2 provider anthropic
3 options {
4 model claude-3-haiku-20240307
5 api_key env.ANTHROPIC_API_KEY
6 max_tokens 1000
7 supports_streaming false
8 }
9}
10
11function MyFunction() -> string {
12 client MyClientWithoutStreaming
13 prompt #"Write a short story"#
14}
1# This will be streamed from your python code perspective,
2# but under the hood it will call the non-streaming HTTP API
3# and then return a streamable response with a single event
4b.stream.MyFunction()
5
6# This will work exactly the same as before
7b.MyFunction()

Forwarded options

system
DO NOT USE

BAML will auto construct this field for you from the prompt, if necessary. Only the first system message will be used, all subsequent ones will be cast to the assistant role.

messages
DO NOT USE

BAML will auto construct this field for you from the prompt

stream
DO NOT USE

BAML will auto construct this field for you based on how you call the client in your code

model
string

The model to use.

Model
claude-3-5-sonnet-20240620
claude-3-opus-20240229
claude-3-sonnet-20240229
claude-3-haiku-20240307

See anthropic docs for the latest list of all models. You can pass any model name you wish, we will not check if it exists.

max_tokens
int

The maximum number of tokens to generate. Default: 4069

For all other options, see the official anthropic API documentation.