# 请求构建

`Context` 怎么组装归[上游](https://fzkuji.github.io/OpenProgram/docs/reference/design/context/README.zh.html)。新增提供商只动这一层和它的 `provider.json`。

```
Context { system_prompt, messages, tools }  --model.api-->  provider translation  -->  wire request
```

## 翻译

| 风格 | system | 对话 | 工具 |
| --- | --- | --- | --- |
| anthropic-messages | 独立的 `system` | `messages` | `tools`，strict 在工具对象内 |
| openai-completions | system 或 developer 消息放在 `messages[0]` | `messages` | `tools`，strict 是布尔值 |
| openai-responses、codex | `instructions` | `input` | `tools` |
| google（gemini） | `systemInstruction`；assistant 变 model | `contents`，内容叫 parts | `tools` |
| bedrock | 独立 system | `messages` | `tools` |

翻译还处理思考块签名、工具参数是对象还是字符串，以及工具 schema 方言（strict、`additionalProperties`）。

## 缓存

| 模式 | 机制 | 声明者 |
| --- | --- | --- |
| `explicit` | 请求里放断点（`cache_control`、`cachePoint`） | anthropic、amazon_bedrock |
| `auto` | 自动前缀缓存，可选 `prompt_cache_key` | openai_responses |
| `out_of_band` | 用单独 API 存缓存；只读命中统计 | 代码认识此模式，目前没有提供商声明 |
| `none` | 不做 | 其余提供商，默认 |

各提供商在 `provider.json` 的 `cache` 键下声明；独立的 `cache.json` 仍能加载，但有弃用警告。`settings/cache_spec.py`：`get_cache_spec`、`cache_mode`、`ttl_for_retention`、`cache_key_param`、`with_lifetime_setting`。

```
{ "mode": "explicit", "breakpoint_format": "cache_control",
  "retention_ttl_map": {"short": null, "long": "1h"},
  "long_ttl_endpoints": ["api.anthropic.com"], "max_breakpoints": 4 }
{ "mode": "auto", "cache_key_param": "prompt_cache_key" }
```

### 断点

| 规则 | 细节 |
| --- | --- |
| 调用方标记 | 内容块上的 `cache_control` 原样透传；`Tool.cache_control` 同样（[透传方案](https://fzkuji.github.io/OpenProgram/docs/reference/design/plans/cache-control-passthrough.html)） |
| 未标记 | `apply_cache_policy`（`settings/cache_policy.py`）标记最后一个不带 `defer_loading` 的工具和最新用户消息的最后文本块，否则其最后一块；调用方的标记保留 |
| 预算 | `max_breakpoints`（4），先给工具，再给消息 |
| TTL | `≥ 3600 秒` 为 `1h`，否则提供商默认 5 分钟；`models.prompt_cache_ttl` 设为 `1h` 时普通缓存活一小时 |
| system | 单个字符串，由各提供商的 `_build_system` 标记整块；策略只管工具和消息 |
| Anthropic | `stream_simple` 构建前应用策略；`_get_cache_control` 读 TTL 映射和 `long_ttl_endpoints` |
| Bedrock | `explicit`，但有自己的 `cachePoint` 块和最后消息逻辑，不走 `apply_cache_policy` |

策略来自 opencode 的 `cache-policy.ts` 和 `protocols/utils/cache.ts`。opencode 的 system 是数组，还能标记最后一段。这里不新增 Context 级结构。
