2222nest_asyncio .apply ()
2323
2424
25+ # Override the SDK default of 2. Each retry uses the SDK's built-in jittered
26+ # exponential backoff and honors `Retry-After`, so 8 attempts ride through
27+ # typical 429/529 capacity blips (~1–2 minutes) instead of failing in ~1.5s.
28+ _MAX_RETRIES = 8
29+
30+
2531# ── Per-provider builders ──
2632
2733def _build_openai (cfg ):
34+ from openai import AsyncOpenAI
2835 from pydantic_ai .models .openai import OpenAIModel
2936 from pydantic_ai .providers .openai import OpenAIProvider
30- return OpenAIModel (cfg .ai_model , provider = OpenAIProvider (api_key = cfg .llm_api_key ))
37+ client = AsyncOpenAI (api_key = cfg .llm_api_key , max_retries = _MAX_RETRIES )
38+ return OpenAIModel (cfg .ai_model , provider = OpenAIProvider (openai_client = client ))
3139
3240
3341def _build_anthropic (cfg ):
42+ from anthropic import AsyncAnthropic
3443 from pydantic_ai .models .anthropic import AnthropicModel
3544 from pydantic_ai .providers .anthropic import AnthropicProvider
36- return AnthropicModel (cfg .ai_model , provider = AnthropicProvider (api_key = cfg .llm_api_key ))
45+ client = AsyncAnthropic (api_key = cfg .llm_api_key , max_retries = _MAX_RETRIES )
46+ return AnthropicModel (cfg .ai_model , provider = AnthropicProvider (anthropic_client = client ))
3747
3848
3949def _build_google (cfg ):
@@ -43,9 +53,11 @@ def _build_google(cfg):
4353
4454
4555def _build_groq (cfg ):
56+ from groq import AsyncGroq
4657 from pydantic_ai .models .groq import GroqModel
4758 from pydantic_ai .providers .groq import GroqProvider
48- return GroqModel (cfg .ai_model , provider = GroqProvider (api_key = cfg .llm_api_key ))
59+ client = AsyncGroq (api_key = cfg .llm_api_key , max_retries = _MAX_RETRIES )
60+ return GroqModel (cfg .ai_model , provider = GroqProvider (groq_client = client ))
4961
5062
5163def _build_mistral (cfg ):
0 commit comments