Skip to content

Commit c33fe23

Browse files
KludexDouweM
andauthored
Set Anthropic max_tokens to 4096 by default (#1994)
Co-authored-by: Douwe Maan <hi@douwe.me>
1 parent 380fbf3 commit c33fe23

File tree

2 files changed

+2
-38
lines changed

2 files changed

+2
-38
lines changed

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
UserPromptPart,
2929
)
3030
from ..profiles import ModelProfileSpec
31-
from ..profiles.anthropic import AnthropicModelProfile
3231
from ..providers import Provider, infer_provider
3332
from ..settings import ModelSettings
3433
from ..tools import ToolDefinition
@@ -217,14 +216,11 @@ async def _messages_create(
217216

218217
system_prompt, anthropic_messages = await self._map_message(messages)
219218

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-
223219
try:
224220
extra_headers = model_settings.get('extra_headers', {})
225221
extra_headers.setdefault('User-Agent', get_user_agent())
226222
return await self.client.beta.messages.create(
227-
max_tokens=max_tokens,
223+
max_tokens=model_settings.get('max_tokens', 4096),
228224
system=system_prompt or NOT_GIVEN,
229225
messages=anthropic_messages,
230226
model=self._model_name,
Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,8 @@
11
from __future__ import annotations as _annotations
22

3-
from dataclasses import dataclass
4-
53
from . import ModelProfile
64

75

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-
326
def anthropic_model_profile(model_name: str) -> ModelProfile | None:
337
"""Get the model profile for an Anthropic model."""
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)
8+
return None

0 commit comments

Comments
 (0)