# 优化prompt来提高模型API调用的Prefill命中率

**URL:** <https://discuss.vllm.ai/t/prompt-api-prefill/1231>\
**Category:** General\
**Created:** [July 30, 2025, 6:13am UTC](https://discuss.vllm.ai/t/prompt-api-prefill/1231 "2025-07-30T06:13:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![XiaoDouGeGe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.vllm.ai/xiaodougege/32/382_2.png) [@XiaoDouGeGe](https://discuss.vllm.ai/u/XiaoDouGeGe)\
**Post date:** [July 30, 2025, 6:13am UTC](https://discuss.vllm.ai/t/prompt-api-prefill/1231/1 "2025-07-30T06:13:50Z")

</div>

是不是可以通过优化调用侧的prompt来提高prefill命中率？有没有prompt的最佳实践或者参考建议，有没有相关的评测工具。

---

<div class="post-metadata">

**Author:** ![RunLLM](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.vllm.ai/runllm/32/233_2.png) [@RunLLM](https://discuss.vllm.ai/u/RunLLM)\
**Post date:** [July 30, 2025, 6:14am UTC](https://discuss.vllm.ai/t/prompt-api-prefill/1231/2 "2025-07-30T06:14:07Z")

</div>

目前vLLM的prefill（prompt阶段）性能主要受prompt长度、批量大小、硬件和底层优化影响，而不是prompt内容本身。通过批量合并、prefix caching等技术可以提升prefill效率，但没有公开资料显示“优化prompt内容”本身能显著提升prefill命中率或速度。vLLM支持prefix caching，可通过合理复用长prompt前缀提升效率，但这依赖于请求间前缀的重合度，而非prompt内容的“最佳时间”或特定写法。[相关讨论](https://github.com/vllm-project/vllm/issues/1635)

目前没有官方推荐的“最佳prompt时间”或内容优化建议，也没有专门针对prompt prefill命中率的评测工具。prefill性能评测通常通过整体吞吐量、延迟等指标衡量，可参考[性能测试方法](https://github.com/vllm-project/vllm/pull/2296)和[官方文档](https://docs.vllm.ai/en/stable/index.html)。如需进一步提升prefill效率，建议关注prefix caching、chunked prefill等特性。

需要更详细的原理或优化建议吗？

Sources:

- [Prompt caching · Issue #1635](https://github.com/vllm-project/vllm/issues/1635)
- [Prefix Prompt Cache · Pull Request #2296](https://github.com/vllm-project/vllm/pull/2296)
- [官方文档](https://docs.vllm.ai/en/stable/index.html)

---

<div class="post-metadata">

**Author:** ![XiaoDouGeGe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.vllm.ai/xiaodougege/32/382_2.png) [@XiaoDouGeGe](https://discuss.vllm.ai/u/XiaoDouGeGe)\
**Post date:** [July 30, 2025, 6:47am UTC](https://discuss.vllm.ai/t/prompt-api-prefill/1231/3 "2025-07-30T06:47:05Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/vllm/original/1X/ed5d528e74f3f3443aa7744950fa9a3aa7e38365.png)

OpenAI有一些最佳实践，也应该适应于vLLM API

---

<div class="post-metadata">

**Author:** ![RunLLM](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.vllm.ai/runllm/32/233_2.png) [@RunLLM](https://discuss.vllm.ai/u/RunLLM)\
**Post date:** [July 30, 2025, 6:47am UTC](https://discuss.vllm.ai/t/prompt-api-prefill/1231/4 "2025-07-30T06:47:41Z")

</div>

这些OpenAI的最佳实践同样适用于vLLM的prefix caching机制：

1. 将静态或重复内容放在prompt开头，动态内容放在结尾，可最大化前缀缓存复用率。
2. 监控cache命中率和延迟，及时调整prompt结构和缓存策略。
3. 在低峰期发送长prompt有助于缓存保留，因高峰期缓存更易被清理。
4. 保持prompt一致性，频繁复用相似前缀可减少缓存失效。[相关原理说明](https://docs.vllm.ai/en/stable/features/automatic_prefix_caching.html#example-workloads)

vLLM的prefix caching本质与OpenAI类似，遵循这些结构化和一致性原则能有效提升缓存命中率和推理效率。[官方设计文档](https://docs.vllm.ai/en/stable/design/v1/prefix_caching.html#searchbox)也建议合理利用前缀重用。

需要更详细的操作建议或代码示例吗？

Sources:

- [vLLM自动前缀缓存原理与建议](https://docs.vllm.ai/en/stable/features/automatic_prefix_caching.html#example-workloads)
- [vLLM前缀缓存设计文档](https://docs.vllm.ai/en/stable/design/v1/prefix_caching.html#searchbox)
