{"id":72,"date":"2026-06-29T10:22:12","date_gmt":"2026-06-29T02:22:12","guid":{"rendered":"https:\/\/genetop.top\/index.php\/2026\/06\/29\/llm-agentic-loop-message-strategy\/"},"modified":"2026-06-29T10:22:12","modified_gmt":"2026-06-29T02:22:12","slug":"llm-agentic-loop-message-strategy","status":"publish","type":"post","link":"https:\/\/genetop.top\/index.php\/2026\/06\/29\/llm-agentic-loop-message-strategy\/","title":{"rendered":"\u522b\u6025\u7740\u6e05 History\u2014\u2014\u7406\u89e3 KV Cache \u540e\u4f60\u4f1a\u91cd\u65b0\u770b\u5f85 LLM \u5bf9\u8bdd\u7b56\u7565"},"content":{"rendered":"<blockquote>\n<p>\u5f88\u591a\u4eba\u5728\u4f7f\u7528 LLM \u65f6\u6709\u4e2a\u76f4\u89c9\uff1a\u5bf9\u8bdd\u8d8a\u957f token \u8d8a\u8d35\uff0c\u5e94\u8be5\u5c3d\u65e9\u505a\u6458\u8981\u538b\u7f29 history\u3002\u5728\u6784\u5efa Agent Loop \u65f6\uff0c\u4e5f\u6709\u4eba\u628a\u591a\u8f6e\u5bf9\u8bdd\u5408\u5e76\u6210\u4e00\u6761&#8221;\u65e0\u72b6\u6001\u6d88\u606f&#8221;\u6765\u7701 token\u3002\u8fd9\u4e24\u79cd\u505a\u6cd5\u770b\u4f3c\u806a\u660e\uff0c\u5b9e\u9645\u4e0a\u90fd\u662f\u53cd\u4f18\u5316\u3002\u672c\u6587\u4ece KV Cache \u539f\u7406\u51fa\u53d1\uff0c\u89e3\u91ca\u4e3a\u4ec0\u4e48\u4fdd\u6301\u539f\u59cb history \u4e0d\u52a8\u624d\u662f\u6700\u4f18\u7b56\u7565\u3002<\/p>\n<\/blockquote>\n<h2 id=\"history\">\u6700\u5e38\u89c1\u7684\u8bef\u533a\uff1a\u4e3b\u52a8\u505a\u6458\u8981\u538b\u7f29 History<\/h2>\n<h3 id=\"_1\">\u573a\u666f<\/h3>\n<p>\u4f60\u548c LLM \u804a\u4e86 20 \u8f6e\uff0ccontext window \u7528\u4e86 8K\/128K\u3002\u4f60\u5f00\u59cb\u7126\u8651\uff1a&#8221;\u8fd9\u4e48\u957f\u7684 history\uff0c\u6bcf\u6b21\u8bf7\u6c42\u90fd\u8981\u53d1\u8fc7\u53bb\uff0c\u592a\u6d6a\u8d39 token \u4e86\u5427\uff1f&#8221;<\/p>\n<p>\u4e8e\u662f\u4f60\u505a\u4e86\u4e00\u4e2a&#8221;\u4f18\u5316&#8221;\uff1a\u8ba9 LLM \u628a\u524d\u9762\u7684\u5bf9\u8bdd\u603b\u7ed3\u6210\u4e00\u6bb5\u6458\u8981\uff0c\u7136\u540e\u7528\u6458\u8981\u5f00\u542f\u65b0\u5bf9\u8bdd\u3002<\/p>\n<pre><code>\u539f\u59cb\u5bf9\u8bdd\uff0820\u8f6e\uff0c8000 token\uff09:\n  [system] [user_1] [asst_1] [user_2] [asst_2] ... [user_20] [asst_20]\n\n&quot;\u4f18\u5316&quot;\u540e\uff08\u6458\u8981\uff0c500 token\uff09:\n  [system] [user: \u4ee5\u4e0b\u662f\u4e4b\u524d\u5bf9\u8bdd\u7684\u6458\u8981\uff1a...500\u5b57...]  [user_21]\n<\/code><\/pre>\n<p>\u770b\u8d77\u6765\u8f93\u5165\u4ece 8000 token \u51cf\u5c11\u5230\u4e86 600 token\uff0c\u7701\u4e86 93%\uff1f<\/p>\n<h3 id=\"_2\">\u4e3a\u4ec0\u4e48\u8fd9\u662f\u53cd\u4f18\u5316<\/h3>\n<p><strong>1. \u4f60\u6467\u6bc1\u4e86 KV Cache<\/strong><\/p>\n<p>\u539f\u59cb\u5bf9\u8bdd\u4e2d\uff0c\u524d 19 \u8f6e\u7684 KV \u5728\u4e0a\u6b21\u8bf7\u6c42\u65f6\u5df2\u7ecf\u7b97\u597d\u5e76\u7f13\u5b58\u5728 GPU \u663e\u5b58\u4e2d\u3002\u7b2c 21 \u8f6e\u8bf7\u6c42\u5230\u6765\u65f6\uff1a<\/p>\n<pre><code>\u539f\u59cb\u65b9\u5f0f:\n  [system][user_1][asst_1]...[user_20][asst_20] \u2190 \u5168\u90e8 cache \u547d\u4e2d(0\u8ba1\u7b97)\n  [user_21]                                      \u2190 \u53ea\u7b97\u8fd9\u4e00\u6761(\u51e0\u5341token)\n\n\u6458\u8981\u65b9\u5f0f:\n  [system][\u6458\u8981...500 token][user_21]            \u2190 \u5168\u65b0\u5185\u5bb9\uff0c\u5168\u90e8\u91cd\u7b97(550 token)\n<\/code><\/pre>\n<p>\u539f\u59cb\u65b9\u5f0f\u5b9e\u9645\u53ea\u9700\u8ba1\u7b97\u51e0\u5341\u4e2a token\uff08\u65b0\u6d88\u606f\uff09\uff0c\u6458\u8981\u65b9\u5f0f\u53cd\u800c\u8981\u8ba1\u7b97 550 \u4e2a token\u3002<strong>\u4f60\u4e3a\u4e86&#8221;\u7701 token&#8221;\uff0c\u5236\u9020\u4e86\u5341\u500d\u7684\u8ba1\u7b97\u5f00\u9500\u3002<\/strong><\/p>\n<p><strong>2. \u6458\u8981\u672c\u8eab\u5c31\u662f\u989d\u5916\u5f00\u9500<\/strong><\/p>\n<p>\u505a\u6458\u8981\u65f6\uff0c\u867d\u7136\u4e4b\u524d\u7684 8000 token \u88ab cache \u8986\u76d6\uff08\u8ba1\u7b97\u5f00\u9500\u5c0f\uff09\uff0c\u4f46\u4f60\u4ecd\u7136\u9700\u8981 LLM \u751f\u6210 500 token \u7684\u6458\u8981\u8f93\u51fa\u3002\u66f4\u5173\u952e\u7684\u662f\uff0c\u8fd9 500 token \u7684\u6458\u8981\u5728\u65b0\u5bf9\u8bdd\u4e2d\u4f1a\u4f5c\u4e3a\u5168\u65b0 input \u518d\u88ab\u5b8c\u6574\u8ba1\u7b97\u4e00\u6b21\uff08\u6ca1\u6709\u4efb\u4f55 cache\uff09\u3002\u76f8\u5f53\u4e8e\u4f60\u4e3a\u4e86&#8221;\u7701 token&#8221;\uff0c\u5148\u82b1 500 token \u751f\u6210\u6458\u8981\uff0c\u518d\u82b1 500 token \u91cd\u65b0\u8ba1\u7b97\u6458\u8981\u2014\u2014\u51c0\u589e\u4e86\u5f00\u9500\u3002<\/p>\n<p><strong>3. \u4fe1\u606f\u4e0d\u53ef\u9006\u4e22\u5931<\/strong><\/p>\n<p>\u6458\u8981\u65f6\u4f60\u65e0\u6cd5\u9884\u5224\u540e\u7eed\u5bf9\u8bdd\u9700\u8981\u54ea\u4e9b\u7ec6\u8282\u3002LLM \u53ef\u80fd\u5728\u7b2c 30 \u8f6e\u9700\u8981\u7b2c 3 \u8f6e\u7684\u67d0\u4e2a\u5177\u4f53\u53c2\u6570\uff0c\u4f46\u6458\u8981\u65f6\u5df2\u7ecf\u4e22\u4e86\u3002<\/p>\n<h3 id=\"_3\">\u6b63\u786e\u7684\u5fc3\u667a\u6a21\u578b<\/h3>\n<pre><code>\u5df2\u6709\u7684 history = \u514d\u8d39\u7684\uff08\u88ab KV Cache \u8986\u76d6\uff0c0 \u8ba1\u7b97\u5f00\u9500\uff09\n\u53ea\u6709\u65b0\u589e\u7684\u5c3e\u90e8 = \u5b9e\u9645\u8ba1\u7b97\u5f00\u9500\n<\/code><\/pre>\n<p>\u6253\u4e2a\u6bd4\u65b9\uff1a\u4f60\u5728\u770b\u4e00\u672c 200 \u9875\u7684\u4e66\uff0c\u5df2\u7ecf\u770b\u5230\u7b2c 180 \u9875\u3002\u6bcf\u6b21\u7ffb\u65b0\u4e00\u9875\u53ea\u9700\u8981\u8bfb 1 \u9875\u5185\u5bb9\u3002\u5982\u679c\u4f60\u8fd9\u65f6\u5019\u628a\u524d 180 \u9875\u6495\u6389\uff0c\u5199\u4e86\u4e00\u9875\u6458\u8981\uff0c\u7136\u540e\u58f0\u79f0&#8221;\u6211\u53ea\u9700\u8981\u8bfb 1 \u9875\u6458\u8981\u5c31\u591f\u4e86&#8221;\u2014\u2014\u4f46\u4f60\u672c\u6765\u5c31\u53ea\u9700\u8981\u8bfb 1 \u9875\u65b0\u5185\u5bb9\u554a\uff01\u6495\u4e66\u8fd9\u4e2a\u64cd\u4f5c\u672c\u8eab\u8fd8\u6d6a\u8d39\u4e86\u65f6\u95f4\u3002<\/p>\n<h3 id=\"_4\">\u4ec0\u4e48\u65f6\u5019\u624d\u5e94\u8be5\u505a\u6458\u8981\uff1f<\/h3>\n<p><strong>\u53ea\u6709\u5f53\u4f60\u771f\u7684\u5feb\u649e\u5230 context window \u4e0a\u9650\u65f6\u3002<\/strong> \u6bd4\u5982 128K \u7684\u7a97\u53e3\u5df2\u7ecf\u7528\u4e86 120K\uff0c\u518d\u52a0\u65b0\u6d88\u606f\u5c31\u6ea2\u51fa\u4e86\u2014\u2014\u8fd9\u65f6\u5019\u522b\u65e0\u9009\u62e9\uff0c\u5fc5\u987b\u538b\u7f29\u3002<\/p>\n<p>\u4f46\u5728\u6b64\u4e4b\u524d\uff08\u6bd4\u5982\u53ea\u7528\u4e86 10%~50%\uff09\uff0c\u4fdd\u6301\u539f\u59cb history \u4e0d\u52a8\u5c31\u662f\u6700\u4f18\u7b56\u7565\u3002\u4e0d\u8981\u548c KV Cache \u4f5c\u5bf9\u3002<\/p>\n<h3 id=\"api\">\u5bf9 API \u8ba1\u8d39\u7684\u5f71\u54cd<\/h3>\n<p>\u4f60\u53ef\u80fd\u60f3\u8bf4\uff1a&#8221;\u5373\u4f7f cache \u547d\u4e2d\uff0cAPI \u63d0\u4f9b\u5546\u4e0d\u8fd8\u662f\u6309 input token \u6570\u6536\u8d39\u5417\uff1f&#8221;<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u4e3b\u6d41\u5382\u5546\u90fd\u5df2\u7ecf\u5bf9 cached token \u63d0\u4f9b\u5927\u5e45\u6298\u6263\uff08\u8fdc\u4e0d\u6b62\u534a\u4ef7\uff09\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5382\u5546<\/th>\n<th>\u6a21\u578b<\/th>\n<th style=\"text-align: right;\">New Input Token<\/th>\n<th style=\"text-align: right;\">Cached Input Token<\/th>\n<th style=\"text-align: right;\">\u7f13\u5b58\u6298\u6263<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><a href=\"https:\/\/openai.com\/api\/pricing\/\">OpenAI<\/a><\/td>\n<td>GPT-5 \u7cfb\u5217<\/td>\n<td style=\"text-align: right;\">$1.25<\/td>\n<td style=\"text-align: right;\">$0.125<\/td>\n<td style=\"text-align: right;\">90%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/openai.com\/api\/pricing\/\">OpenAI<\/a><\/td>\n<td>GPT-4.1<\/td>\n<td style=\"text-align: right;\">$2.00<\/td>\n<td style=\"text-align: right;\">$0.50<\/td>\n<td style=\"text-align: right;\">75%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/openai.com\/api\/pricing\/\">OpenAI<\/a><\/td>\n<td>GPT-4.1 Mini<\/td>\n<td style=\"text-align: right;\">$0.40<\/td>\n<td style=\"text-align: right;\">$0.10<\/td>\n<td style=\"text-align: right;\">75%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.anthropic.com\/api\">Anthropic<\/a><\/td>\n<td>Claude Sonnet 4.x<\/td>\n<td style=\"text-align: right;\">$3.00<\/td>\n<td style=\"text-align: right;\">$0.30<\/td>\n<td style=\"text-align: right;\">90%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.anthropic.com\/api\">Anthropic<\/a><\/td>\n<td>Claude Opus 4.x<\/td>\n<td style=\"text-align: right;\">$15.00<\/td>\n<td style=\"text-align: right;\">$1.50<\/td>\n<td style=\"text-align: right;\">90%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.anthropic.com\/api\">Anthropic<\/a><\/td>\n<td>Claude Haiku<\/td>\n<td style=\"text-align: right;\">$0.80<\/td>\n<td style=\"text-align: right;\">$0.08<\/td>\n<td style=\"text-align: right;\">90%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/ai.google.dev\/pricing\">Google AI Studio<\/a><\/td>\n<td>Gemini 2.5 Pro<\/td>\n<td style=\"text-align: right;\">$1.25<\/td>\n<td style=\"text-align: right;\">$0.125<\/td>\n<td style=\"text-align: right;\">90%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/ai.google.dev\/pricing\">Google AI Studio<\/a><\/td>\n<td>Gemini 2.5 Flash<\/td>\n<td style=\"text-align: right;\">$0.15<\/td>\n<td style=\"text-align: right;\">$0.015<\/td>\n<td style=\"text-align: right;\">90%<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/ai.google.dev\/pricing\">Google AI Studio<\/a><\/td>\n<td>Gemini 2.0 Flash<\/td>\n<td style=\"text-align: right;\">$0.10<\/td>\n<td style=\"text-align: right;\">$0.025<\/td>\n<td style=\"text-align: right;\">75%<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56fd\u5185\u5382\u5546\u7684\u7f13\u5b58\u6298\u6263\u901a\u5e38\u66f4\u6fc0\u8fdb\uff0c\u5c24\u5176\u662f DeepSeek \u7cfb\uff08cached token \u4ef7\u683c\u4f4e\u81f3 new token \u7684 1\/10 \u751a\u81f3\u66f4\u4f4e\uff09\u3002<\/p>\n<p><strong>\u8fd9\u610f\u5473\u7740\uff1a<\/strong> \u5728 API \u8ba1\u8d39\u5c42\u9762\uff0c\u4fdd\u6301\u539f\u59cb history \u4e0d\u52a8\u540c\u6837\u662f\u7ecf\u6d4e\u7684\u3002\u5047\u8bbe\u4f60\u6709 8000 token \u7684 history\uff1a<\/p>\n<ul>\n<li>\u4fdd\u6301\u539f\u6837\uff1a8000 \u00d7 cached \u4ef7\u683c\uff081~2.5 \u6298\uff09+ \u65b0\u6d88\u606f \u00d7 \u5168\u4ef7<\/li>\n<li>\u505a\u6458\u8981\u66ff\u6362\uff1a500 \u00d7 \u5168\u4ef7\uff08\u6458\u8981\u662f\u65b0\u5185\u5bb9\uff0c\u65e0 cache\uff09+ \u65b0\u6d88\u606f \u00d7 \u5168\u4ef7 + \u6458\u8981\u751f\u6210\u7684\u8f93\u51fa\u8d39\u7528<\/li>\n<\/ul>\n<p>\u8868\u9762\u4e0a 8000 \u2192 500 \u770b\u4f3c\u7701\u4e86\uff0c\u4f46 8000 token \u6309 1 \u6298\u8ba1\u7b97 = \u7b49\u6548 800 token \u5168\u4ef7\u3002\u52a0\u4e0a\u6458\u8981\u8f93\u51fa\u8d39\u7528\u548c\u4fe1\u606f\u4e22\u5931\uff0c\u6536\u76ca\u5fae\u4e4e\u5176\u5fae\u751a\u81f3\u4e3a\u8d1f\u3002<\/p>\n<p>\u5bf9\u4e8e <strong>\u81ea\u90e8\u7f72\u6a21\u578b\uff08vLLM\/TGI\uff09<\/strong>\uff1a\u6ca1\u6709\u6309 token \u8ba1\u8d39\uff0c\u5f00\u9500\u7eaf\u7cb9\u53d6\u51b3\u4e8e GPU \u8ba1\u7b97\u91cf\u3002\u6b64\u65f6\u4fdd\u6301\u539f\u59cb history \u7684\u4f18\u52bf\u662f\u538b\u5012\u6027\u7684\u2014\u2014cache \u547d\u4e2d = \u96f6\u989d\u5916\u8ba1\u7b97\u3002<\/p>\n<h2 id=\"agentic-loop\">Agentic Loop \u4e2d\u7684\u540c\u7c7b\u95ee\u9898<\/h2>\n<p>\u4e0a\u8ff0\u8bef\u533a\u5728 Agent Loop \u8bbe\u8ba1\u4e2d\u6709\u4e00\u4e2a\u53d8\u4f53\uff1a\u628a\u591a\u8f6e\u5de5\u5177\u8c03\u7528\u5386\u53f2\u5408\u5e76\u6210\u4e00\u6761&#8221;\u65e0\u72b6\u6001\u6d88\u606f&#8221;\u6765&#8221;\u7701 token&#8221;\u3002\u63a5\u4e0b\u6765\u7528\u4e00\u4e2a\u5177\u4f53\u4f8b\u5b50\u5206\u6790\u8fd9\u79cd\u505a\u6cd5\u3002<\/p>\n<h3 id=\"_5\">\u80cc\u666f<\/h3>\n<p>\u5728 Agentic RAG \u7684\u8fed\u4ee3\u641c\u7d22\u573a\u666f\u4e2d\uff0cAgent \u6bcf\u8f6e\u8c03\u7528 LLM \u51b3\u5b9a\u4e0b\u4e00\u6b65\u64cd\u4f5c\uff08\u641c\u7d22\u3001\u4e22\u5f03\u3001\u7ed3\u675f\uff09\u3002LLM \u9700\u8981\u77e5\u9053\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u7684\u539f\u59cb\u95ee\u9898<\/li>\n<li>\u4e4b\u524d\u6267\u884c\u4e86\u54ea\u4e9b\u5de5\u5177\u8c03\u7528<\/li>\n<li>\u5f53\u524d\u6536\u96c6\u5230\u4e86\u54ea\u4e9b evidence<\/li>\n<\/ol>\n<p>\u95ee\u9898\u6765\u4e86\uff1a<strong>\u600e\u4e48\u628a\u8fd9\u4e9b\u4fe1\u606f\u4f20\u7ed9 LLM\uff1f<\/strong> \u8fd9\u672c\u8d28\u4e0a\u548c\u4e0a\u9762&#8221;\u8981\u4e0d\u8981\u538b\u7f29 history&#8221;\u662f\u540c\u4e00\u4e2a\u95ee\u9898\u3002<\/p>\n<h2 id=\"_6\">\u4e24\u79cd\u65b9\u5f0f<\/h2>\n<h3 id=\"astateless-merge\">\u65b9\u5f0f A\uff1a\u5168\u91cf\u5408\u5e76\uff08Stateless Merge\uff09<\/h3>\n<p>\u6bcf\u6b21\u8c03\u7528 LLM \u65f6\uff0c\u628a\u6240\u6709\u5386\u53f2\u538b\u7f29\u6210\u4e00\u4e24\u6761 user \u6d88\u606f\uff1a<\/p>\n<pre><code class=\"language-python\">def build_messages():\n    msgs = [\n        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: SYSTEM_PROMPT},\n        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: query},\n    ]\n    # \u5168\u90e8 trace \u5408\u6210\u4e00\u6761\u6587\u672c\n    msgs.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: f&quot;[\u5df2\u6267\u884c\u7684\u5de5\u5177\u8c03\u7528]\\n{trace_text}&quot;})\n    # \u5168\u90e8 evidence \u5408\u6210\u4e00\u6761 JSON\n    msgs.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: f&quot;[\u5f53\u524d evidence]\\n{evidence_json}&quot;})\n    return msgs\n<\/code><\/pre>\n<p><strong>\u52a8\u673a<\/strong>\uff1a\u770b\u8d77\u6765\u6d88\u606f\u5c11\u3001\u7ed3\u6784\u7b80\u5355\uff0c\u4e14\u7701\u6389\u4e86\u5386\u53f2\u4e2d LLM \u7684 assistant \u56de\u590d\uff08\u53ef\u80fd\u5305\u542b\u5197\u957f\u7684 thinking\/reasoning\uff09\uff0c\u76f4\u89c9\u4e0a\u80fd\u7701 token\u3002<\/p>\n<h3 id=\"bstateful-messages\">\u65b9\u5f0f B\uff1a\u6807\u51c6\u591a\u8f6e\u5bf9\u8bdd\uff08Stateful Messages\uff09<\/h3>\n<p>\u4fdd\u6301\u5b8c\u6574\u7684\u5bf9\u8bdd\u7ed3\u6784\uff0c\u6bcf\u8f6e\u8ffd\u52a0 assistant tool_call + tool result\uff1a<\/p>\n<pre><code class=\"language-python\">messages = [\n    {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: SYSTEM_PROMPT},\n    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: query},\n]\n\nfor each iteration:\n    response = llm.chat(messages, tools=...)\n    messages.append(response.message)  # assistant with tool_calls\n    result = execute_tool(response.tool_call)\n    messages.append({&quot;role&quot;: &quot;tool&quot;, &quot;content&quot;: result, &quot;tool_call_id&quot;: ...})\n<\/code><\/pre>\n<h2 id=\"_7\">\u7528\u4e00\u4e2a\u4f8b\u5b50\u8bf4\u6e05\u695a<\/h2>\n<p>\u5047\u8bbe Agent \u8dd1\u4e86 3 \u8f6e\uff0c\u6bcf\u8f6e\u5de5\u5177\u8fd4\u56de\u7ea6 500 token \u7684 evidence\uff0cLLM \u6bcf\u8f6e reasoning \u7ea6 200 token\u3002<\/p>\n<h3 id=\"a-input-tokens\">\u65b9\u5f0f A \u4e09\u8f6e\u7684 input tokens<\/h3>\n<pre><code>\u7b2c 1 \u8f6e: system(100) + user(50)                                     = 150\n\u7b2c 2 \u8f6e: system(100) + user(50) + trace(30) + evidence(500)         = 680\n\u7b2c 3 \u8f6e: system(100) + user(50) + trace(60) + evidence(1000)        = 1210\n                                                          \u603b input = 2040\n<\/code><\/pre>\n<p>\u6bcf\u6b21\u90fd\u662f\u5168\u65b0\u5185\u5bb9 \u2192 <strong>KV Cache \u547d\u4e2d\u7387 \u2248 0%<\/strong> \u2192 2040 token \u5168\u90e8\u9700\u8981 GPU \u4ece\u5934\u8ba1\u7b97\u3002<\/p>\n<h3 id=\"b-input-tokens\">\u65b9\u5f0f B \u4e09\u8f6e\u7684 input tokens<\/h3>\n<pre><code>\u7b2c 1 \u8f6e: system(100) + user(50)                                     = 150\n\u7b2c 2 \u8f6e: system(100) + user(50) + asst_1(200) + tool_1(500)         = 850\n\u7b2c 3 \u8f6e: system(100) + user(50) + asst_1(200) + tool_1(500)\n         + asst_2(200) + tool_2(500)                                = 1550\n                                                          \u603b input = 2550\n<\/code><\/pre>\n<p>\u591a\u4e86 assistant \u6d88\u606f\uff08+400 token\uff09\uff0c\u4f46\u5173\u952e\u533a\u522b\uff1a<\/p>\n<ul>\n<li>\u7b2c 2 \u8f6e\u7684\u524d 150 token \u4e0e\u7b2c 1 \u8f6e\u5b8c\u5168\u76f8\u540c \u2192 <strong>cache \u547d\u4e2d<\/strong><\/li>\n<li>\u7b2c 3 \u8f6e\u7684\u524d 850 token \u4e0e\u7b2c 2 \u8f6e\u5b8c\u5168\u76f8\u540c \u2192 <strong>cache \u547d\u4e2d<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u9645\u9700\u8981\u8ba1\u7b97\u7684 token\uff1a<\/p>\n<pre><code>\u7b2c 1 \u8f6e: 150\uff08\u5168\u7b97\uff09\n\u7b2c 2 \u8f6e: 700\uff08\u524d 150 \u547d\u4e2d cache\uff0c\u53ea\u7b97\u65b0\u589e 700\uff09\n\u7b2c 3 \u8f6e: 700\uff08\u524d 850 \u547d\u4e2d cache\uff0c\u53ea\u7b97\u65b0\u589e 700\uff09\n                                          \u5b9e\u9645\u8ba1\u7b97\u91cf = 1550\n<\/code><\/pre>\n<h3 id=\"_8\">\u5bf9\u6bd4\u8868<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u6307\u6807<\/th>\n<th>\u65b9\u5f0f A\uff08\u5168\u91cf\u5408\u5e76\uff09<\/th>\n<th>\u65b9\u5f0f B\uff08\u6807\u51c6\u591a\u8f6e\uff09<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u603b input token \u6570<\/td>\n<td>2040<\/td>\n<td>2550<\/td>\n<\/tr>\n<tr>\n<td>KV Cache \u547d\u4e2d<\/td>\n<td>0%<\/td>\n<td>~60%<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5b9e\u9645 GPU \u8ba1\u7b97\u91cf<\/strong><\/td>\n<td><strong>2040<\/strong><\/td>\n<td><strong>1550<\/strong><\/td>\n<\/tr>\n<tr>\n<td>LLM \u7406\u89e3\u96be\u5ea6<\/td>\n<td>\u8f83\u9ad8\uff08\u975e\u6807\u51c6\u683c\u5f0f\uff09<\/td>\n<td>\u4f4e\uff08\u539f\u751f\u8bad\u7ec3\u683c\u5f0f\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u7ed3\u8bba\uff1a\u65b9\u5f0f A \u770b\u4f3c token \u5c11\uff0c\u5b9e\u9645\u8ba1\u7b97\u91cf\u66f4\u5927\u3002<\/strong><\/p>\n<h2 id=\"prefilldecode-kv-cache\">\u6df1\u5165\u7406\u89e3\uff1aPrefill\u3001Decode \u4e0e KV Cache<\/h2>\n<h3 id=\"llm\">LLM \u63a8\u7406\u7684\u4e24\u4e2a\u9636\u6bb5<\/h3>\n<p>\u4f60\u4e00\u5b9a\u6ce8\u610f\u8fc7\uff1aLLM \u6536\u5230\u8f93\u5165\u540e\uff0c\u5410\u51fa\u7b2c\u4e00\u4e2a token \u6bd4\u8f83\u6162\uff0c\u540e\u7eed token \u5219\u5237\u5237\u5237\u5f88\u5feb\u3002\u8fd9\u6b63\u662f\u4e24\u4e2a\u9636\u6bb5\u7684\u4f53\u73b0\uff1a<\/p>\n<p><strong>1. Prefill\uff08\u9884\u586b\u5145\uff09<\/strong>\uff1a\u5904\u7406\u6240\u6709 input token\uff0c\u4e3a\u6bcf\u4e2a token \u5728\u6bcf\u4e00\u5c42 Transformer \u8ba1\u7b97\u51fa Key \u548c Value \u5411\u91cf\uff0c\u5b58\u5165 KV Cache\u3002\u8fd9\u662f<strong>\u8ba1\u7b97\u5bc6\u96c6\u578b<\/strong>\u7684\u2014\u2014\u9700\u8981\u5bf9 N \u4e2a token \u505a\u5168\u91cf attention \u77e9\u9635\u8fd0\u7b97\uff0c\u590d\u6742\u5ea6 O(N\u00b2)\u3002<\/p>\n<p><strong>2. Decode\uff08\u89e3\u7801\uff09<\/strong>\uff1a\u9010\u4e2a\u751f\u6210 output token\u3002\u6bcf\u751f\u6210\u4e00\u4e2a\u65b0 token\uff0c\u53ea\u9700\u8981\u7528\u5b83\u7684 Query \u53bb\u548c KV Cache \u4e2d\u5df2\u6709\u7684 Key \u505a attention\uff0c\u590d\u6742\u5ea6 O(N)\u3002\u7136\u540e\u628a\u65b0 token \u7684 K\u3001V \u8ffd\u52a0\u5230 cache \u4e2d\uff0c\u4e3a\u4e0b\u4e00\u4e2a token \u670d\u52a1\u3002<\/p>\n<p>\u6253\u4e2a\u6bd4\u65b9\uff1a<br \/>\n&#8211; Prefill = \u8bfb\u5b8c\u4e00\u6574\u672c\u4e66\uff0c\u505a\u597d\u7b14\u8bb0\uff08\u8017\u65f6\uff0c\u5bf9\u5e94 TTFT \u6162\uff09<br \/>\n&#8211; Decode = \u6839\u636e\u7b14\u8bb0\u9010\u53e5\u5199\u7b54\u6848\uff08\u76f8\u5bf9\u8f7b\u677e\uff0c\u5bf9\u5e94\u540e\u7eed token \u5feb\uff09<\/p>\n<p>\u6240\u4ee5\u4f60\u611f\u53d7\u5230\u7684&#8221;\u5148\u5361\u4e00\u4e0b\uff0c\u7136\u540e\u5237\u5237\u5237\u51fa\u6765&#8221;\u5c31\u662f Prefill \u2192 Decode \u7684\u5206\u754c\u3002<\/p>\n<h3 id=\"kv-cache\">\u4ec0\u4e48\u662f KV Cache\uff1f<\/h3>\n<p>Transformer \u6bcf\u4e00\u5c42\u7684 Self-Attention \u8ba1\u7b97\uff1a<\/p>\n<pre><code>Attention(Q, K, V) = softmax(Q \u00d7 K^T \/ \u221ad) \u00d7 V\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6709 32 \u5c42\u3001\u6bcf\u5c42 Key \u7ef4\u5ea6 128\u3001\u5171 32 \u4e2a attention head \u7684\u6a21\u578b\uff08\u7c7b\u4f3c LLaMA-7B\uff09\uff0c\u5904\u7406 1000 \u4e2a token \u7684 KV Cache \u5927\u5c0f\uff1a<\/p>\n<pre><code>32\u5c42 \u00d7 2(K\u548cV) \u00d7 32\u5934 \u00d7 1000 token \u00d7 128\u7ef4 \u00d7 2\u5b57\u8282(fp16)\n\u2248 512 MB\n<\/code><\/pre>\n<p>\u8fd9\u4e9b K\u3001V \u5411\u91cf\u4e00\u65e6\u7b97\u597d\uff0c\u5728 Decode \u9636\u6bb5\u751f\u6210\u540e\u7eed token \u65f6\u53ef\u4ee5\u53cd\u590d\u590d\u7528\u2014\u2014\u4e0d\u9700\u8981\u5bf9\u5386\u53f2 token \u91cd\u65b0\u8ba1\u7b97\uff0c\u8fd9\u5c31\u662f KV Cache \u7684\u6838\u5fc3\u4ef7\u503c\u3002<\/p>\n<h3 id=\"decode-token\">Decode \u9636\u6bb5\uff1a\u6bcf\u6b21\u53ea\u7b97\u4e00\u4e2a token<\/h3>\n<p>Decode \u65f6\u6bcf\u6b65\u6c38\u8fdc\u53ea\u8ba1\u7b97 <strong>1 \u4e2a\u65b0 token<\/strong> \u7684 Q\/K\/V\u3002\u65b0 token \u7684 KV \u76f4\u63a5\u8ffd\u52a0\u5230 cache \u7684\u4e0b\u4e00\u4e2a slot\uff1a<\/p>\n<pre><code>Block5 (\u5bb9\u91cf16):\n  slot 0: token_a \u7684 KV  \u2190 \u5df2\u7b97\u597d\n  slot 1: token_b \u7684 KV  \u2190 \u5df2\u7b97\u597d\n  slot 2: token_c \u7684 KV  \u2190 \u65b0 token\uff0c\u53ea\u7b97\u8fd9\u4e00\u4e2a\uff0c\u5199\u5165\u8fd9\u91cc\n  slot 3~15: \u7a7a\n<\/code><\/pre>\n<p>Block \u662f KV Cache <strong>\u5b58\u50a8\u7ba1\u7406<\/strong>\u7684\u5355\u4f4d\uff08\u7c7b\u4f3c\u5185\u5b58\u5206\u9875\uff09\uff0c\u4e0d\u662f\u8ba1\u7b97\u5355\u4f4d\u3002\u4e00\u4e2a block \u6ca1\u586b\u6ee1\u65f6\uff0c\u65b0 token \u7684 KV \u76f4\u63a5\u5199\u5165\u540c\u4e00 block \u7684\u4e0b\u4e00\u4e2a slot\uff0c\u4e0d\u5f71\u54cd\u5df2\u6709\u7684\u503c\uff0c\u4e5f\u4e0d\u9700\u8981\u91cd\u7b97\u6574\u4e2a block\u3002<\/p>\n<h3 id=\"prefix-caching\">\u8de8\u8bf7\u6c42\u7684 Prefix Caching<\/h3>\n<p>\u5173\u952e\u6d1e\u5bdf\uff1a<strong>\u5982\u679c\u4e24\u6b21\u8bf7\u6c42\u5171\u4eab\u76f8\u540c\u7684\u524d\u7f00\uff0c\u90a3\u4e48\u524d\u7f00\u90e8\u5206\u7684 KV \u5411\u91cf\u5b8c\u5168\u4e00\u6837\uff0c\u4e0d\u9700\u8981\u91cd\u7b97\u3002<\/strong><\/p>\n<h4 id=\"agent-loop\">\u4e3e\u4f8b\uff1aAgent Loop \u4e2d\u7684\u6807\u51c6\u591a\u8f6e\u5bf9\u8bdd<\/h4>\n<p>\u5047\u8bbe system prompt = &#8220;\u4f60\u662f\u641c\u7d22\u52a9\u624b&#8221;\uff0c\u7528\u6237\u95ee\u9898 = &#8220;\u4ec0\u4e48\u662f GraphRAG\uff1f&#8221;<\/p>\n<p><strong>\u7b2c 1 \u8f6e\u8bf7\u6c42\uff1a<\/strong><\/p>\n<pre><code>[system: \u4f60\u662f\u641c\u7d22\u52a9\u624b] [user: \u4ec0\u4e48\u662fGraphRAG\uff1f]\n \u2190\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 150 token \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2192\n<\/code><\/pre>\n<p>Prefill \u8ba1\u7b97 150 \u4e2a token \u7684 KV \u2192 \u5b58\u5165 cache\uff0ckey = hash(&#8220;\u4f60\u662f\u641c\u7d22\u52a9\u624b|\u4ec0\u4e48\u662fGraphRAG\uff1f&#8221;)<\/p>\n<p>LLM \u8fd4\u56de\uff1a\u8c03\u7528 search({&#8220;query&#8221;: &#8220;GraphRAG&#8221;})<\/p>\n<p><strong>\u7b2c 2 \u8f6e\u8bf7\u6c42\uff1a<\/strong><\/p>\n<pre><code>[system: \u4f60\u662f\u641c\u7d22\u52a9\u624b] [user: \u4ec0\u4e48\u662fGraphRAG\uff1f] [asst: search(...)] [tool: \u7ed3\u679cA]\n \u2190\u2500\u2500\u2500\u2500 \u548c\u7b2c1\u8f6e\u5b8c\u5168\u76f8\u540c \u2500\u2500\u2500\u2500\u2192 \u2190\u2500\u2500\u2500\u2500\u2500\u2500 \u65b0\u589e 700 token \u2500\u2500\u2500\u2500\u2500\u2500\u2192\n \u2190\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 850 token \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2192\n<\/code><\/pre>\n<p>\u63a8\u7406\u5f15\u64ce\u53d1\u73b0\uff1a\u524d 150 token \u7684 hash \u548c\u4e4b\u524d\u7f13\u5b58\u7684\u5339\u914d\uff01<\/p>\n<pre><code>\u5df2\u7f13\u5b58: token 1~150 \u7684 KV\uff08\u76f4\u63a5\u590d\u7528\uff0c0 \u8ba1\u7b97\uff09\n\u9700\u8ba1\u7b97: token 151~850 \u7684 KV\uff08\u53ea\u7b97\u65b0\u589e\u7684 700 token\uff09\n<\/code><\/pre>\n<p><strong>\u7b2c 3 \u8f6e\u8bf7\u6c42\uff1a<\/strong><\/p>\n<pre><code>[\u540c\u4e0a 850 token] [asst: search(...)] [tool: \u7ed3\u679cB]\n \u2190\u2500 cache \u547d\u4e2d \u2500\u2192 \u2190\u2500\u2500 \u65b0\u589e 700 \u2500\u2500\u2192\n<\/code><\/pre>\n<p>cache \u547d\u4e2d 850 token\uff0c\u53ea\u9700\u8ba1\u7b97 700 token\u3002<\/p>\n<h4 id=\"_9\">\u5982\u679c\u7528\u5168\u91cf\u5408\u5e76\u65b9\u5f0f<\/h4>\n<p><strong>\u7b2c 2 \u8f6e\u8bf7\u6c42\uff1a<\/strong><\/p>\n<pre><code>[system: \u4f60\u662f\u641c\u7d22\u52a9\u624b] [user: \u4ec0\u4e48\u662fGraphRAG\uff1f] [user: [\u5df2\u6267\u884c\u5de5\u5177]\\n search\u21925\u6761] [user: [evidence]\\n{...500\u5b57...}]\n \u2190\u2500\u2500\u2500\u2500 \u548c\u7b2c1\u8f6e\u76f8\u540c \u2500\u2500\u2500\u2500\u2192 \u2190\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u5168\u65b0\u5185\u5bb9 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2192\n<\/code><\/pre>\n<p>\u524d 150 token \u5339\u914d\uff0c\u540e\u9762 530 token \u662f\u65b0\u5185\u5bb9\u3002<\/p>\n<p><strong>\u7b2c 3 \u8f6e\u8bf7\u6c42\uff1a<\/strong><\/p>\n<pre><code>[system: \u4f60\u662f\u641c\u7d22\u52a9\u624b] [user: \u4ec0\u4e48\u662fGraphRAG\uff1f] [user: [\u5df2\u6267\u884c\u5de5\u5177]\\n search\u21925\u6761\\n search\u21923\u6761] [user: [evidence]\\n{...1000\u5b57...}]\n \u2190\u2500\u2500\u2500\u2500 \u548c\u7b2c1\u8f6e\u76f8\u540c \u2500\u2500\u2500\u2500\u2192 \u2190\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u5185\u5bb9\u53d8\u4e86\uff01\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2192\n<\/code><\/pre>\n<p>\u7b2c 3 \u4e2a\u6d88\u606f\u7684\u5185\u5bb9\u4ece <code>\"search\u21925\u6761\"<\/code> \u53d8\u6210\u4e86 <code>\"search\u21925\u6761\\n search\u21923\u6761\"<\/code>\u2014\u2014<strong>\u4ece\u8fd9\u91cc\u5f00\u59cb cache \u5168\u90e8\u5931\u6548<\/strong>\uff1a<\/p>\n<pre><code>cache \u547d\u4e2d: 150 token\uff08\u53ea\u6709 system + user query\uff09\n\u9700\u8981\u8ba1\u7b97: 1060 token\n<\/code><\/pre>\n<p>\u5bf9\u6bd4\u65b9\u5f0f B \u540c\u4e00\u8f6e\u53ea\u9700\u8ba1\u7b97 700 token\u3002\u5dee\u8ddd\u968f\u7740\u8fed\u4ee3\u8f6e\u6570\u589e\u52a0\u8d8a\u6765\u8d8a\u5927\u3002<\/p>\n<h3 id=\"_10\">\u524d\u7f00\u5339\u914d\u7684\u4e25\u683c\u987a\u5e8f\u6027<\/h3>\n<p>Prefix caching \u662f<strong>\u4ece\u5934\u9010\u5757\u987a\u5e8f\u5339\u914d<\/strong>\u7684\u3002\u539f\u56e0\u662f attention \u673a\u5236\u4e2d\u7684 positional encoding\u2014\u2014\u540c\u4e00\u4e2a token \u51fa\u73b0\u5728\u4f4d\u7f6e 0 \u548c\u4f4d\u7f6e 16\uff0c\u5176 KV \u503c\u662f\u4e0d\u540c\u7684\u3002<\/p>\n<p>\u8fd9\u610f\u5473\u7740\uff1a<strong>\u5982\u679c\u5728\u5f00\u5934\u63d2\u5165\u4e86\u65b0 token\uff0c\u6574\u4e2a cache \u5931\u6548\uff0c\u5168\u90e8\u91cd\u7b97\u3002<\/strong><\/p>\n<pre><code>\u7f13\u5b58\u4e2d: [block0][block1][block2][block3][block4]\n\u65b0\u8bf7\u6c42: [new_block][block0'][block1'][block2'][block5][block6]\n            \u2717 \u2192 \u7b2c\u4e00\u4e2a\u5757\u5c31\u4e0d\u5339\u914d\uff0c\u540e\u9762\u5373\u4f7f\u5185\u5bb9\u76f8\u540c\u4e5f\u65e0\u6cd5\u590d\u7528\n<\/code><\/pre>\n<p>\u4e0d\u53ef\u80fd\u8df3\u8fc7\u524d\u9762\u53bb\u5339\u914d\u540e\u9762\u2014\u2014\u4f4d\u7f6e\u53d8\u4e86\uff0cKV \u503c\u5c31\u53d8\u4e86\u3002<\/p>\n<p>\u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48 <strong>system prompt \u653e\u5728\u6700\u524d\u9762\u662f\u6709\u5229\u7684<\/strong>\u2014\u2014\u5b83\u662f\u6240\u6709\u8bf7\u6c42\u5171\u4eab\u7684\u56fa\u5b9a\u524d\u7f00\uff0c\u4fdd\u8bc1\u4e86\u5f00\u5934\u90e8\u5206\u7684 cache \u59cb\u7ec8\u547d\u4e2d\u3002<\/p>\n<h3 id=\"prefix-caching-vllm\">Prefix Caching \u7684\u5b9e\u73b0\u673a\u5236\uff08vLLM\uff09<\/h3>\n<ol>\n<li><strong>\u5206\u5757\u54c8\u5e0c<\/strong>\uff1a\u5c06 token \u5e8f\u5217\u6309\u56fa\u5b9a\u5927\u5c0f\uff08\u5982 16 token\uff09\u5206\u5757\uff0c\u5bf9\u6bcf\u5757\u5185\u5bb9\u8ba1\u7b97 hash<\/li>\n<li><strong>\u9010\u5757\u5339\u914d<\/strong>\uff1a\u65b0\u8bf7\u6c42\u5230\u6765\u65f6\uff0c\u4ece\u5934\u9010\u5757\u6bd4\u5bf9 hash\uff0c\u627e\u5230\u6700\u957f\u5339\u914d\u524d\u7f00<\/li>\n<li><strong>\u590d\u7528 KV Block<\/strong>\uff1a\u5339\u914d\u7684\u5757\u76f4\u63a5\u5f15\u7528\u5df2\u7f13\u5b58\u7684 GPU \u663e\u5b58\u4e2d\u7684 KV \u6570\u636e<\/li>\n<li><strong>\u53ea\u8ba1\u7b97\u5c3e\u90e8<\/strong>\uff1a\u4ece\u7b2c\u4e00\u4e2a\u4e0d\u5339\u914d\u7684\u5757\u5f00\u59cb\u505a prefill<\/li>\n<\/ol>\n<pre><code>\u5df2\u7f13\u5b58\u7684\u8bf7\u6c42: [block0][block1][block2][block3][block4]\n\u65b0\u8bf7\u6c42:       [block0][block1][block2][block5][block6]\n                 \u2713       \u2713       \u2713      \u2717 \u2192 \u4ece\u8fd9\u91cc\u5f00\u59cb\u8ba1\u7b97\n<\/code><\/pre>\n<h3 id=\"_11\">\u53ef\u89c6\u5316\u5bf9\u6bd4<\/h3>\n<pre><code>\u65b9\u5f0f B\uff08\u6807\u51c6\u591a\u8f6e\uff09\u2014\u2014 \u6bcf\u8f6e\u53ea\u8ba1\u7b97\u5c3e\u90e8\u65b0\u589e\u90e8\u5206\n\n\u7b2c1\u8f6e: [\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588]                    \u8ba1\u7b97 150\n\u7b2c2\u8f6e: [--------][\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588]    \u8ba1\u7b97 700  (\u524d150 cache\u547d\u4e2d)\n\u7b2c3\u8f6e: [--------------------][\u2588\u2588\u2588\u2588]  \u8ba1\u7b97 700  (\u524d850 cache\u547d\u4e2d)\n                              \u603b\u8ba1\u7b97\u91cf = 1550\n\n\u65b9\u5f0f A\uff08\u5168\u91cf\u5408\u5e76\uff09\u2014\u2014 \u6bcf\u8f6e\u4ece\u7b2c3\u6761\u6d88\u606f\u5f00\u59cb\u5c31\u53d8\u4e86\n\n\u7b2c1\u8f6e: [\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588]                    \u8ba1\u7b97 150\n\u7b2c2\u8f6e: [--------][\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588]    \u8ba1\u7b97 530  (\u524d150 cache\u547d\u4e2d)\n\u7b2c3\u8f6e: [--------][\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588]  \u8ba1\u7b97 1060 (\u524d150 cache\u547d\u4e2d,\u540e\u9762\u5168\u53d8\u4e86)\n                              \u603b\u8ba1\u7b97\u91cf = 1740\n<\/code><\/pre>\n<p>\u968f\u7740\u8f6e\u6570\u589e\u52a0\uff0c\u65b9\u5f0f A \u7684\u52a3\u52bf\u52a0\u901f\u653e\u5927\u3002<\/p>\n<h2 id=\"a\">\u65b9\u5f0f A \u8fd8\u6709\u54ea\u4e9b\u9690\u85cf\u4ee3\u4ef7\uff1f<\/h2>\n<h3 id=\"1\">1. \u6a21\u578b\u7406\u89e3\u80fd\u529b\u4e0b\u964d<\/h3>\n<p>LLM \u5728\u8bad\u7ec3\u65f6\u89c1\u5230\u7684 tool use \u683c\u5f0f\u662f\uff1a<\/p>\n<pre><code>assistant: I'll search for... [tool_call: search({query: &quot;...&quot;})]\ntool: [results...]\nassistant: Based on results, I'll now... [tool_call: ...]\n<\/code><\/pre>\n<p>\u4f60\u7528\u7eaf\u6587\u672c\u6a21\u62df\u8fd9\u4e2a\u8fc7\u7a0b\uff1a<\/p>\n<pre><code>user: [\u5df2\u6267\u884c\u7684\u5de5\u5177\u8c03\u7528]\n  [0] search({&quot;query&quot;: &quot;...&quot;}) \u2192 5 results\n  [1] search({&quot;query&quot;: &quot;...&quot;}) \u2192 3 results\n<\/code><\/pre>\n<p>\u6a21\u578b\u9700\u8981\u989d\u5916\u7684&#8221;\u8ba4\u77e5\u5f00\u9500&#8221;\u6765\u7406\u89e3\u8fd9\u79cd\u975e\u6807\u51c6\u683c\u5f0f\uff0c\u53ef\u80fd\u5bfc\u81f4\uff1a<br \/>\n&#8211; \u91cd\u590d\u8c03\u7528\u5df2\u6267\u884c\u7684\u5de5\u5177\uff08\u56e0\u4e3a\u7ed3\u6784\u4e0d\u5982\u539f\u751f\u683c\u5f0f\u6e05\u6670\uff09<br \/>\n&#8211; \u65e0\u6cd5\u6b63\u786e\u5224\u65ad\u54ea\u4e9b\u4fe1\u606f\u6765\u81ea\u5de5\u5177\u3001\u54ea\u4e9b\u6765\u81ea\u7528\u6237<\/p>\n<h3 id=\"2\">2. \u65e0\u6cd5\u8868\u8fbe\u5de5\u5177\u5931\u8d25<\/h3>\n<p>\u6807\u51c6\u65b9\u5f0f\u4e2d\uff0c\u5de5\u5177\u5931\u8d25\u53ef\u4ee5\u660e\u786e\u8fd4\u56de\uff1a<\/p>\n<pre><code class=\"language-json\">{&quot;role&quot;: &quot;tool&quot;, &quot;content&quot;: &quot;Error: timeout after 10s&quot;, &quot;tool_call_id&quot;: &quot;...&quot;}\n<\/code><\/pre>\n<p>LLM \u770b\u5230\u540e\u4f1a\u8c03\u6574\u7b56\u7565\u3002\u65b9\u5f0f A \u4e2d\u53ea\u80fd\u5199 <code>\u2192 0 results<\/code>\uff0cLLM \u5206\u4e0d\u6e05&#8221;\u6ca1\u641c\u5230&#8221;\u548c&#8221;\u641c\u7d22\u51fa\u9519\u4e86&#8221;\u3002<\/p>\n<h3 id=\"3-tool-call\">3. \u4e27\u5931\u5e76\u884c tool call \u80fd\u529b<\/h3>\n<p>\u6807\u51c6\u683c\u5f0f\u652f\u6301\u4e00\u6b21\u8fd4\u56de\u591a\u4e2a tool_calls\uff0c\u63a8\u7406\u5f15\u64ce\u77e5\u9053\u5b83\u4eec\u662f\u540c\u4e00\u8f6e\u7684\u5e76\u884c\u8c03\u7528\u3002\u65b9\u5f0f A \u7684\u6241\u5e73 trace \u6587\u672c\u65e0\u6cd5\u8868\u8fbe\u8fd9\u79cd\u7ed3\u6784\u3002<\/p>\n<h2 id=\"a_1\">\u4ec0\u4e48\u65f6\u5019\u65b9\u5f0f A \u6709\u4f18\u52bf\uff1f<\/h2>\n<p>\u516c\u5e73\u5730\u8bf4\uff0c\u5b58\u5728\u5c11\u6570\u573a\u666f\u65b9\u5f0f A \u66f4\u5408\u7406\uff1a<\/p>\n<ol>\n<li><strong>\u63a8\u7406\u5f15\u64ce\u4e0d\u652f\u6301 prefix caching<\/strong>\uff08\u5c11\u89c1\uff0c\u4e3b\u6d41\u5f15\u64ce\u90fd\u652f\u6301\uff09<\/li>\n<li><strong>\u6bcf\u8f6e assistant reasoning \u6781\u957f<\/strong>\uff08\u5982 DeepSeek \u7684 thinking \u52a8\u8f84 2000+ token\uff09\uff0c\u4e14\u4f60\u786e\u5b9a\u8fd9\u4e9b reasoning \u5bf9\u540e\u7eed\u51b3\u7b56\u65e0\u5e2e\u52a9<\/li>\n<li><strong>\u9700\u8981\u8de8 session \u6062\u590d<\/strong>\u2014\u2014\u65e0\u72b6\u6001\u8bbe\u8ba1\u5141\u8bb8\u4ece\u4efb\u610f\u4e2d\u95f4\u72b6\u6001\u6062\u590d\uff0c\u4e0d\u4f9d\u8d56\u5b8c\u6574\u5bf9\u8bdd\u5386\u53f2<\/li>\n<\/ol>\n<p>\u5bf9\u4e8e\u7b2c 2 \u70b9\uff0c\u66f4\u597d\u7684\u505a\u6cd5\u662f\uff1a\u4fdd\u6301\u6807\u51c6\u591a\u8f6e\u683c\u5f0f\uff0c\u4f46\u5728\u8ffd\u52a0\u5386\u53f2 assistant \u6d88\u606f\u65f6<strong>\u622a\u65ad reasoning \u90e8\u5206<\/strong>\uff0c\u53ea\u4fdd\u7559 tool_call \u7ed3\u6784\u3002\u8fd9\u6837\u65e2\u7701 token \u53c8\u4fdd\u7559 cache \u548c\u683c\u5f0f\u4f18\u52bf\u3002<\/p>\n<h2 id=\"_12\">\u63a8\u8350\u505a\u6cd5<\/h2>\n<pre><code class=\"language-python\">messages = [\n    {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: SYSTEM_PROMPT},\n    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: query},\n]\n\nfor i in range(max_iterations):\n    response = await llm.chat.completions.create(\n        model=model, messages=messages, tools=tools_schema\n    )\n    assistant_msg = response.choices[0].message\n\n    if not assistant_msg.tool_calls:\n        break\n\n    # \u8ffd\u52a0 assistant \u6d88\u606f\uff08\u53ef\u9009\uff1a\u622a\u65ad reasoning \u4ee5\u7701 token\uff09\n    messages.append(assistant_msg.model_dump())\n\n    # \u6267\u884c\u5de5\u5177\u5e76\u8ffd\u52a0\u7ed3\u679c\n    for tool_call in assistant_msg.tool_calls:\n        result = await execute(tool_call)\n        messages.append({\n            &quot;role&quot;: &quot;tool&quot;,\n            &quot;tool_call_id&quot;: tool_call.id,\n            &quot;content&quot;: json.dumps(result, ensure_ascii=False),\n        })\n<\/code><\/pre>\n<p>\u7b80\u6d01\u3001\u6807\u51c6\u3001cache \u53cb\u597d\u3002<\/p>\n<h2 id=\"_13\">\u603b\u7ed3<\/h2>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>\u5168\u91cf\u5408\u5e76<\/th>\n<th>\u6807\u51c6\u591a\u8f6e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Token \u6570\u91cf<\/td>\n<td>\u7565\u5c11<\/td>\n<td>\u7565\u591a<\/td>\n<\/tr>\n<tr>\n<td>\u5b9e\u9645\u63a8\u7406\u5f00\u9500<\/td>\n<td><strong>\u66f4\u5927<\/strong>\uff08\u65e0 cache\uff09<\/td>\n<td><strong>\u66f4\u5c0f<\/strong>\uff08cache \u547d\u4e2d\u9ad8\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u6a21\u578b\u7406\u89e3\u51c6\u786e\u5ea6<\/td>\n<td>\u8f83\u5dee<\/td>\n<td>\u597d\uff08\u539f\u751f\u683c\u5f0f\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u5de5\u7a0b\u590d\u6742\u5ea6<\/td>\n<td>\u9700\u624b\u52a8\u5e8f\u5217\u5316<\/td>\n<td>\u6846\u67b6\u539f\u751f\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u89c2\u6d4b\u6027<\/td>\n<td>\u5dee\uff08\u4e22\u5931\u7ed3\u6784\uff09<\/td>\n<td>\u597d\uff08\u6bcf\u8f6e\u6e05\u6670\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u4e0d\u8981\u4e3a\u4e86\u7701\u51e0\u767e token \u800c\u653e\u5f03 KV Cache \u548c\u539f\u751f\u683c\u5f0f\u5e26\u6765\u7684\u5de8\u5927\u4f18\u52bf\u3002<\/strong> \u8868\u9762\u4e0a\u7684&#8221;\u4f18\u5316&#8221;\u5b9e\u9645\u4e0a\u662f\u53cd\u4f18\u5316\u2014\u2014\u5c31\u50cf\u4e3a\u4e86\u7701\u5185\u5b58\u800c\u5173\u6389 CPU cache\uff0c\u5f97\u4e0d\u507f\u5931\u3002<\/p>\n<h2 id=\"_14\">\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h2>\n<blockquote>\n<p><strong>\u5df2\u6709\u7684 history \u662f\u514d\u8d39\u7684\uff0c\u65b0\u589e\u7684\u5185\u5bb9\u624d\u662f\u5f00\u9500\u3002\u522b\u4e3b\u52a8\u6467\u6bc1 cache\u3002<\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u4eba\u5728\u4f7f\u7528 LLM \u65f6\u6709\u4e2a\u76f4\u89c9\uff1a\u5bf9\u8bdd\u8d8a\u957f token \u8d8a\u8d35\uff0c\u5e94\u8be5\u5c3d\u65e9\u505a\u6458\u8981\u538b\u7f29 history\u3002\u5728\u6784\u5efa Ag [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[120],"tags":[115,112,113,114,117,119,118,116],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-llm","tag-agentic-loop","tag-kv-cache","tag-llm","tag-prefix-caching","tag-token","tag-transformer","tag-vllm","tag-116"],"_links":{"self":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":0,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"wp:attachment":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}