You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/mkdocs/en/model.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ Models in tRPC-Agent have the following core features:
10
10
-**Streaming response support**: Supports streaming output for real-time interactive experiences
11
11
-**Multimodal capabilities**: Supports multimodal content processing including text, images, etc. (e.g., Hunyuan multimodal models)
12
12
-**Prompt Cache support**: Provides unified prompt cache configuration across OpenAI, Anthropic, and LiteLLM routes to reduce repeated input cost for long prompts and multi-turn conversations
13
+
-**Model retry support**: Supports configuring retry at the model layer. The SDK automatically retries when exceptions such as rate limits occur, and backs off using an exponential backoff strategy
13
14
-**Extensible configuration**: Supports custom configuration options such as GenerateContentConfig, HttpOptions, client_args to meet various scenario requirements
14
15
15
16
## Quick Start
@@ -514,6 +515,76 @@ Different model services report different fields. OpenAI-compatible endpoints us
514
515
515
516
For a complete runnable example, see [examples/llmagent_with_prompt_cache](../../../examples/llmagent_with_prompt_cache/README.md).
516
517
518
+
### Model Retry
519
+
520
+
Model retry is useful when LLM requests encounter transient failures such as rate limits, timeouts, network fluctuations, or temporary service unavailability. By passing `ModelRetryConfig` when constructing a model, you can configure retry behavior at the model layer. When retryable exceptions occur, the SDK automatically retries the request and backs off according to the configured exponential backoff strategy.
521
+
522
+
`OpenAIModel`, `AnthropicModel`, and `LiteLLMModel` all support model retry. This capability is disabled by default and is enabled only when `model_retry_config` is explicitly provided.
523
+
524
+
#### Basic usage
525
+
526
+
```python
527
+
from trpc_agent_sdk.configs import ExponentialBackoffConfig
528
+
from trpc_agent_sdk.configs import ModelRetryConfig
529
+
from trpc_agent_sdk.models import OpenAIModel
530
+
531
+
model = OpenAIModel(
532
+
model_name="deepseek-chat",
533
+
api_key="your-api-key",
534
+
base_url="https://api.deepseek.com/v1",
535
+
model_retry_config=ModelRetryConfig(
536
+
num_retries=2, # Additional retry attempts after the initial request fails
537
+
backoff=ExponentialBackoffConfig(
538
+
initial_backoff=1.0, # Base delay in seconds before the first retry
539
+
max_backoff=8.0, # Upper bound in seconds for a single retry delay
540
+
multiplier=2.0, # Exponential backoff multiplier
541
+
jitter=True, # Whether to apply full jitter to avoid synchronized retries
542
+
),
543
+
),
544
+
)
545
+
```
546
+
547
+
#### Configuration fields
548
+
549
+
| Field | Default | Description |
550
+
|-------|---------|-------------|
551
+
|`num_retries`|`2`| Additional retry attempts after the initial request fails; does not include the initial request. Set to `0` to disable additional retries |
552
+
|`backoff.initial_backoff`|`1.0`| Base delay in seconds before the first retry |
553
+
|`backoff.max_backoff`|`10.0`| Upper bound in seconds for a single retry delay |
554
+
|`backoff.multiplier`|`2.0`| Exponential backoff multiplier, e.g. retry delays are computed as `initial_backoff * multiplier^attempt`|
555
+
|`backoff.jitter`|`True`| Whether to apply full jitter. When enabled, the SDK waits for a random duration between `0` and the current backoff delay to avoid synchronized retries |
556
+
557
+
If the provider returns `Retry-After` or `retry-after-ms`, and the suggested delay is within a reasonable range, the SDK uses the server-suggested delay first. Otherwise, it computes the delay from the exponential backoff configuration.
558
+
559
+
#### Retry decisions
560
+
561
+
The SDK combines response headers, HTTP status codes, and provider SDK exception semantics to decide whether to retry. The general precedence is:
562
+
563
+
-`x-should-retry: true` forces the error to be treated as retryable, while `x-should-retry: false` forces it to be treated as non-retryable.
564
+
- HTTP status `408`, `409`, `429`, and `>=500` are generally retryable.
565
+
- Other `4xx` errors such as `400`, `401`, `403`, and `404` are generally not retried.
566
+
- Timeout and connection-related OpenAI / Anthropic exceptions are generally retryable.
567
+
-`LiteLLMModel` primarily uses the header and status information on LiteLLM-normalized exceptions.
568
+
569
+
#### Streaming behavior
570
+
571
+
To avoid duplicated output, model retry only happens before the current model call has produced user-visible content (text or tool calls). If a streaming response has already emitted partial content and then fails, the SDK returns a final error response directly instead of replaying the whole request.
572
+
573
+
Runner code does not need additional retry handling:
574
+
575
+
```python
576
+
asyncfor event in runner.run_async(
577
+
user_id=user_id,
578
+
session_id=session_id,
579
+
new_message=user_content,
580
+
):
581
+
...
582
+
```
583
+
584
+
If the model call encounters a retryable error before producing content, the SDK retries according to `ModelRetryConfig`. If the retry budget is exhausted or the error is non-retryable, the SDK surfaces an `LlmResponse` with `error_code` / `error_message`.
585
+
586
+
For a complete runnable example, see [examples/llmagent_with_model_retry](../../../examples/llmagent_with_model_retry/README.md).
587
+
517
588
### Custom HTTP Headers
518
589
519
590
Pass additional headers via `client_args`'s `default_headers` or `GenerateContentConfig`'s `HttpOptions`, suitable for gateways, proprietary platforms, or proxy environments. For example:
0 commit comments