Skip to content

Commit ae0c3ce

Browse files
authored
Set Anthropic max_tokens to the highest allowed by the model by default (#1979)
1 parent 477a590 commit ae0c3ce

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
UserPromptPart,
2929
)
3030
from ..profiles import ModelProfileSpec
31+
from ..profiles.anthropic import AnthropicModelProfile
3132
from ..providers import Provider, infer_provider
3233
from ..settings import ModelSettings
3334
from ..tools import ToolDefinition
@@ -216,11 +217,14 @@ async def _messages_create(
216217

217218
system_prompt, anthropic_messages = await self._map_message(messages)
218219

220+
max_tokens_limit = AnthropicModelProfile.from_profile(self.profile).anthropic_max_tokens_limit
221+
max_tokens = min(model_settings.get('max_tokens', max_tokens_limit), max_tokens_limit)
222+
219223
try:
220224
extra_headers = model_settings.get('extra_headers', {})
221225
extra_headers.setdefault('User-Agent', get_user_agent())
222226
return await self.client.beta.messages.create(
223-
max_tokens=model_settings.get('max_tokens', 1024),
227+
max_tokens=max_tokens,
224228
system=system_prompt or NOT_GIVEN,
225229
messages=anthropic_messages,
226230
model=self._model_name,
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
from __future__ import annotations as _annotations
22

3+
from dataclasses import dataclass
4+
35
from . import ModelProfile
46

57

8+
@dataclass
9+
class AnthropicModelProfile(ModelProfile):
10+
"""Profile for models used with AnthropicModel.
11+
12+
ALL FIELDS MUST BE `anthropic_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.
13+
"""
14+
15+
anthropic_max_tokens_limit: int = 1024
16+
"""The maximum number of tokens that can be generated by this model, as documented at https://docs.anthropic.com/en/docs/about-claude/models/overview. Defaults to 1024."""
17+
18+
19+
MAX_TOKENS = {
20+
'claude-3-haiku': 4096,
21+
'claude-3-opus': 4096,
22+
'claude-3-5-haiku': 8192,
23+
'claude-3-5-sonnet': 8192,
24+
'claude-3-7-sonnet': 64000,
25+
'claude-opus-4': 32000,
26+
'claude-4-opus': 32000,
27+
'claude-sonnet-4': 64000,
28+
'claude-4-sonnet': 64000,
29+
}
30+
31+
632
def anthropic_model_profile(model_name: str) -> ModelProfile | None:
733
"""Get the model profile for an Anthropic model."""
8-
return None
34+
max_tokens = 1024
35+
for model_prefix, model_max_tokens in MAX_TOKENS.items():
36+
if model_name.startswith(model_prefix):
37+
max_tokens = model_max_tokens
38+
break
39+
40+
return AnthropicModelProfile(anthropic_max_tokens_limit=max_tokens)

0 commit comments

Comments
 (0)