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
+115Lines changed: 115 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,7 @@ Models in tRPC-Agent have the following core features:
9
9
-**Multi-protocol support**: Provides OpenAIModel, AnthropicModel, LiteLLMModel, etc., compatible with most OpenAI-like and Anthropic interfaces both internally and externally
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
+
-**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
12
13
-**Extensible configuration**: Supports custom configuration options such as GenerateContentConfig, HttpOptions, client_args to meet various scenario requirements
13
14
14
15
## Quick Start
@@ -398,6 +399,120 @@ LlmAgent(
398
399
)
399
400
```
400
401
402
+
### Prompt Cache
403
+
404
+
Prompt Cache is useful when system prompts are long, tool definitions are large, or multi-turn conversations share a stable prefix. Many providers, including OpenAI-compatible serving stacks such as `openai/sglang`, already support automatic prefix caching on the server side. `tRPC-Agent` does not replace the provider's cache implementation; instead, it provides unified management hints and normalized observability for prompt cache behavior.
405
+
406
+
`tRPC-Agent` exposes these capabilities through `PromptCacheConfig`, which currently applies to `OpenAIModel`, `AnthropicModel`, and provider-prefixed `LiteLLMModel`. Because providers expose different cache controls and usage fields, the SDK maps management options and cache usage metrics to each provider protocol on a best-effort basis:
| Anthropic | Manages explicit `cache_control` breakpoints according to `breakpoints`|`cache_read_input_tokens`, `cache_creation_input_tokens`|
411
+
| OpenAI / OpenAI-compatible endpoints | Passes cache hints such as `prompt_cache_key` / `prompt_cache_retention` when supported; provider-side automatic prefix caching still owns cache creation and lookup | Usually only `cache_read_input_tokens`|
412
+
| LiteLLM | Chooses the Anthropic-style or OpenAI-style cache management path according to the `provider/model` prefix, while preserving provider-native automatic caching such as `openai/sglang`| Depends on the final provider route |
413
+
414
+
#### Model-level configuration
415
+
416
+
Model-level configuration becomes the default prompt-cache management and observability configuration for the model instance. Use it when all requests can share the same cache hints:
417
+
418
+
```python
419
+
from trpc_agent_sdk.configs import PromptCacheConfig
420
+
from trpc_agent_sdk.models import OpenAIModel
421
+
422
+
model = OpenAIModel(
423
+
model_name="gpt-4o",
424
+
api_key="your-api-key",
425
+
prompt_cache_config=PromptCacheConfig(
426
+
enabled=True,
427
+
ttl="24h",
428
+
prompt_cache_key="weather-concierge-v1",
429
+
),
430
+
)
431
+
```
432
+
433
+
#### Per-run override
434
+
435
+
You can also override prompt-cache settings for a single `runner.run_async()` call through `RunConfig.prompt_cache`. The per-run config overrides model-level settings field by field, which is useful when setting different cache hints by user, tenant, or business scenario:
436
+
437
+
```python
438
+
from trpc_agent_sdk.configs import PromptCacheConfig
439
+
from trpc_agent_sdk.configs import RunConfig
440
+
441
+
asyncfor event in runner.run_async(
442
+
user_id=user_id,
443
+
session_id=session_id,
444
+
new_message=user_content,
445
+
run_config=RunConfig(
446
+
prompt_cache=PromptCacheConfig(
447
+
enabled=True,
448
+
prompt_cache_key="weather-concierge-user-42",
449
+
),
450
+
),
451
+
):
452
+
...
453
+
```
454
+
455
+
#### Anthropic breakpoints
456
+
457
+
Anthropic-style caching requires selecting breakpoint locations. `breakpoints` supports the following values:
458
+
459
+
-`"system"`: cache the system prompt, suitable for long instructions
460
+
-`"tools"`: cache the last tool definition, suitable when tools are numerous or tool schemas are large
461
+
-`"messages"`: cache the most recent assistant message, suitable for growing stable history prefixes in multi-turn conversations
462
+
463
+
```python
464
+
from trpc_agent_sdk.configs import PromptCacheConfig
465
+
from trpc_agent_sdk.models import AnthropicModel
466
+
467
+
model = AnthropicModel(
468
+
model_name="claude-3-5-sonnet-20241022",
469
+
api_key="your-api-key",
470
+
prompt_cache_config=PromptCacheConfig(
471
+
enabled=True,
472
+
ttl="1h",
473
+
breakpoints=["tools", "system", "messages"],
474
+
),
475
+
)
476
+
```
477
+
478
+
A good starting point is `["tools", "system"]`; add `"messages"` when long multi-turn conversations need to cache a growing history prefix. Some Anthropic proxies or Bedrock routes require a minimum cache block size, so short prompts may not create cache entries.
479
+
480
+
#### LiteLLM routes
481
+
482
+
When using `LiteLLMModel`, the model name should include a `provider/model` prefix. The SDK uses that provider prefix to select the appropriate cache-management mapping. For example:
483
+
484
+
```python
485
+
from trpc_agent_sdk.configs import PromptCacheConfig
486
+
from trpc_agent_sdk.models import LiteLLMModel
487
+
488
+
model = LiteLLMModel(
489
+
model_name="openai/gpt-4o",
490
+
api_key="your-api-key",
491
+
prompt_cache_config=PromptCacheConfig(
492
+
enabled=True,
493
+
prompt_cache_key="shared-prefix-v1",
494
+
),
495
+
)
496
+
```
497
+
498
+
If the model name does not include a provider prefix, the SDK cannot determine which cache-management protocol to use, so SDK-managed cache hints may not take effect.
499
+
500
+
#### Reading cache usage
501
+
502
+
The model response's `usage_metadata` normalizes cache usage fields where possible:
503
+
504
+
```python
505
+
asyncfor event in runner.run_async(...):
506
+
usage =getattr(event, "usage_metadata", None)
507
+
if usage:
508
+
print(usage.cache_read_input_tokens) # Input tokens read from cache
509
+
print(usage.cache_creation_input_tokens) # Input tokens written to cache, usually only reported by Anthropic
510
+
print(usage.prompt_token_count) # Total input tokens
511
+
```
512
+
513
+
Different model services report different fields. OpenAI-compatible endpoints usually report cache reads but not cache writes. With load-balanced proxies, each backend instance may have its own KV cache and may not be warmed up at the same time, so cache hit rates can fluctuate during the first few runs.
514
+
515
+
For a complete runnable example, see [examples/llmagent_with_prompt_cache](../../../examples/llmagent_with_prompt_cache/README.md).
0 commit comments