|
| 1 | +import asyncio |
| 2 | +import inspect |
| 3 | + |
| 4 | +from agents import ( |
| 5 | + Agent, |
| 6 | + ModelRetrySettings, |
| 7 | + ModelSettings, |
| 8 | + RetryDecision, |
| 9 | + RunConfig, |
| 10 | + Runner, |
| 11 | + retry_policies, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def format_error(error: object) -> str: |
| 16 | + if not isinstance(error, BaseException): |
| 17 | + return "Unknown error" |
| 18 | + return str(error) or error.__class__.__name__ |
| 19 | + |
| 20 | + |
| 21 | +async def main() -> None: |
| 22 | + apply_policies = retry_policies.any( |
| 23 | + # On OpenAI-backed models, provider_suggested() follows provider retry advice, |
| 24 | + # including fallback retryable statuses when x-should-retry is absent |
| 25 | + # (for example 408/409/429/5xx). |
| 26 | + retry_policies.provider_suggested(), |
| 27 | + retry_policies.retry_after(), |
| 28 | + retry_policies.network_error(), |
| 29 | + retry_policies.http_status([408, 409, 429, 500, 502, 503, 504]), |
| 30 | + ) |
| 31 | + |
| 32 | + async def policy(context) -> bool | RetryDecision: |
| 33 | + raw_decision = apply_policies(context) |
| 34 | + decision: bool | RetryDecision |
| 35 | + if inspect.isawaitable(raw_decision): |
| 36 | + decision = await raw_decision |
| 37 | + else: |
| 38 | + decision = raw_decision |
| 39 | + if isinstance(decision, RetryDecision): |
| 40 | + if not decision.retry: |
| 41 | + print( |
| 42 | + f"[retry] stop after attempt {context.attempt}/{context.max_retries + 1}: " |
| 43 | + f"{format_error(context.error)}" |
| 44 | + ) |
| 45 | + return False |
| 46 | + |
| 47 | + print( |
| 48 | + " | ".join( |
| 49 | + part |
| 50 | + for part in [ |
| 51 | + f"[retry] retry attempt {context.attempt}/{context.max_retries + 1}", |
| 52 | + ( |
| 53 | + f"waiting {decision.delay:.2f}s" |
| 54 | + if decision.delay is not None |
| 55 | + else "using default backoff" |
| 56 | + ), |
| 57 | + f"reason: {decision.reason}" if decision.reason else None, |
| 58 | + f"error: {format_error(context.error)}", |
| 59 | + ] |
| 60 | + if part is not None |
| 61 | + ) |
| 62 | + ) |
| 63 | + return decision |
| 64 | + |
| 65 | + if not decision: |
| 66 | + print( |
| 67 | + f"[retry] stop after attempt {context.attempt}/{context.max_retries + 1}: " |
| 68 | + f"{format_error(context.error)}" |
| 69 | + ) |
| 70 | + return decision |
| 71 | + |
| 72 | + retry = ModelRetrySettings( |
| 73 | + max_retries=4, |
| 74 | + backoff={ |
| 75 | + "initial_delay": 0.5, |
| 76 | + "max_delay": 5.0, |
| 77 | + "multiplier": 2.0, |
| 78 | + "jitter": True, |
| 79 | + }, |
| 80 | + policy=policy, |
| 81 | + ) |
| 82 | + |
| 83 | + # RunConfig-level model_settings are shared defaults for the run. |
| 84 | + # If an Agent also defines model_settings, the Agent wins for overlapping |
| 85 | + # keys, while nested objects like retry/backoff are merged. |
| 86 | + run_config = RunConfig(model_settings=ModelSettings(retry=retry)) |
| 87 | + |
| 88 | + agent = Agent( |
| 89 | + name="Assistant", |
| 90 | + instructions="You are a concise assistant. Answer in 3 short bullet points at most.", |
| 91 | + # Prefix with litellm/ to route this request through the LiteLLM adapter. |
| 92 | + model="litellm/openai/gpt-4o-mini", |
| 93 | + # This Agent repeats the same retry config for clarity. In real code you |
| 94 | + # can keep shared defaults in RunConfig and only put per-agent overrides |
| 95 | + # here when you need different retry behavior. |
| 96 | + model_settings=ModelSettings(retry=retry), |
| 97 | + ) |
| 98 | + |
| 99 | + print( |
| 100 | + "Retry support is configured. You will only see [retry] logs if a transient failure happens." |
| 101 | + ) |
| 102 | + |
| 103 | + result = await Runner.run( |
| 104 | + agent, |
| 105 | + "Explain exponential backoff for API retries in plain English.", |
| 106 | + run_config=run_config, |
| 107 | + ) |
| 108 | + |
| 109 | + print("\nFinal output:\n") |
| 110 | + print(result.final_output) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + asyncio.run(main()) |
0 commit comments