{"id":104,"date":"2026-03-21T22:47:17","date_gmt":"2026-03-21T14:47:17","guid":{"rendered":"https:\/\/pa.yingzhi8.cn\/index.php\/2026\/03\/21\/concepts-session-pruning\/"},"modified":"2026-03-21T23:08:54","modified_gmt":"2026-03-21T15:08:54","slug":"concepts-session-pruning","status":"publish","type":"post","link":"https:\/\/pa.yingzhi8.cn\/index.php\/2026\/03\/21\/concepts-session-pruning\/","title":{"rendered":"Session pruning"},"content":{"rendered":"<h1>Session pruning<\/h1>\n<h1>\u4f1a\u8bdd\u526a\u679d<\/h1>\n<p>\u4f1a\u8bdd\u526a\u679d\u5728\u6bcf\u6b21 LLM \u8c03\u7528\u4e4b\u524d\u4ece\u5185\u5b58\u4e0a\u4e0b\u6587\u4e2d\u4fee\u526a<strong>\u65e7\u7684\u5de5\u5177\u7ed3\u679c<\/strong>\u3002\u5b83<strong>\u4e0d\u4f1a<\/strong>\u91cd\u5199\u78c1\u76d8\u4e0a\u7684\u4f1a\u8bdd\u5386\u53f2\uff08<code>*.jsonl<\/code>\uff09\u3002<\/p>\n<h2>\u8fd0\u884c\u65f6\u673a<\/h2>\n<ul>\n<li>\u5f53\u542f\u7528 <code>mode: \"cache-ttl\"<\/code> \u4e14\u8be5\u4f1a\u8bdd\u7684\u6700\u540e\u4e00\u6b21 Anthropic \u8c03\u7528\u65e9\u4e8e <code>ttl<\/code> \u65f6\u3002<\/li>\n<li>\u4ec5\u5f71\u54cd\u8be5\u8bf7\u6c42\u53d1\u9001\u7ed9\u6a21\u578b\u7684\u6d88\u606f\u3002<\/li>\n<li>\u4ec5\u5bf9 Anthropic API \u8c03\u7528\uff08\u548c OpenRouter Anthropic \u6a21\u578b\uff09\u751f\u6548\u3002<\/li>\n<li>\u4e3a\u83b7\u5f97\u6700\u4f73\u6548\u679c\uff0c\u8bf7\u5c06 <code>ttl<\/code> \u4e0e\u4f60\u7684\u6a21\u578b <code>cacheControlTtl<\/code> \u5339\u914d\u3002<\/li>\n<li>\u526a\u679d\u540e\uff0cTTL \u7a97\u53e3\u4f1a\u91cd\u7f6e\uff0c\u56e0\u6b64\u540e\u7eed\u8bf7\u6c42\u4f1a\u4fdd\u6301\u7f13\u5b58\u76f4\u5230 <code>ttl<\/code> \u518d\u6b21\u8fc7\u671f\u3002<\/li>\n<\/ul>\n<h2>\u667a\u80fd\u9ed8\u8ba4\u503c\uff08Anthropic\uff09<\/h2>\n<ul>\n<li><strong>OAuth \u6216 setup-token<\/strong> \u914d\u7f6e\u6587\u4ef6\uff1a\u542f\u7528 <code>cache-ttl<\/code> \u526a\u679d\u5e76\u5c06\u5fc3\u8df3\u8bbe\u7f6e\u4e3a <code>1h<\/code>\u3002<\/li>\n<li><strong>API \u5bc6\u94a5<\/strong>\u914d\u7f6e\u6587\u4ef6\uff1a\u542f\u7528 <code>cache-ttl<\/code> \u526a\u679d\uff0c\u5c06\u5fc3\u8df3\u8bbe\u7f6e\u4e3a <code>30m<\/code>\uff0c\u5e76\u5c06 Anthropic \u6a21\u578b\u7684 <code>cacheControlTtl<\/code> \u9ed8\u8ba4\u4e3a <code>1h<\/code>\u3002<\/li>\n<li>\u5982\u679c\u4f60\u663e\u5f0f\u8bbe\u7f6e\u4e86\u8fd9\u4e9b\u503c\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\uff0cOpenClaw <strong>\u4e0d\u4f1a<\/strong>\u8986\u76d6\u5b83\u4eec\u3002<\/li>\n<\/ul>\n<h2>\u6539\u8fdb\u5185\u5bb9\uff08\u6210\u672c + \u7f13\u5b58\u884c\u4e3a\uff09<\/h2>\n<ul>\n<li><strong>\u4e3a\u4ec0\u4e48\u8981\u526a\u679d\uff1a<\/strong> Anthropic \u63d0\u793a\u7f13\u5b58\u4ec5\u5728 TTL \u5185\u9002\u7528\u3002\u5982\u679c\u4f1a\u8bdd\u7a7a\u95f2\u8d85\u8fc7 TTL\uff0c\u4e0b\u4e00\u4e2a\u8bf7\u6c42\u4f1a\u91cd\u65b0\u7f13\u5b58\u5b8c\u6574\u63d0\u793a\uff0c\u9664\u975e\u4f60\u5148\u4fee\u526a\u5b83\u3002<\/li>\n<li><strong>\u4ec0\u4e48\u53d8\u5f97\u66f4\u4fbf\u5b9c\uff1a<\/strong> \u526a\u679d\u51cf\u5c11\u4e86 TTL \u8fc7\u671f\u540e\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u7684 <strong>cacheWrite<\/strong> \u5927\u5c0f\u3002<\/li>\n<li><strong>\u4e3a\u4ec0\u4e48 TTL \u91cd\u7f6e\u5f88\u91cd\u8981\uff1a<\/strong> \u4e00\u65e6\u526a\u679d\u8fd0\u884c\uff0c\u7f13\u5b58\u7a97\u53e3\u4f1a\u91cd\u7f6e\uff0c\u56e0\u6b64\u540e\u7eed\u8bf7\u6c42\u53ef\u4ee5\u91cd\u7528\u65b0\u7f13\u5b58\u7684\u63d0\u793a\uff0c\u800c\u4e0d\u662f\u518d\u6b21\u91cd\u65b0\u7f13\u5b58\u5b8c\u6574\u5386\u53f2\u3002<\/li>\n<li><strong>\u5b83\u4e0d\u505a\u4ec0\u4e48\uff1a<\/strong> \u526a\u679d\u4e0d\u4f1a\u6dfb\u52a0 token \u6216&#8221;\u53cc\u500d&#8221;\u6210\u672c\uff1b\u5b83\u53ea\u6539\u53d8\u8be5 TTL \u540e\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u7f13\u5b58\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<h2>\u53ef\u4ee5\u526a\u679d\u7684\u5185\u5bb9<\/h2>\n<ul>\n<li>\u4ec5 <code>toolResult<\/code> \u6d88\u606f\u3002<\/li>\n<li>\u7528\u6237 + \u52a9\u624b\u6d88\u606f<strong>\u6c38\u8fdc\u4e0d\u4f1a<\/strong>\u88ab\u4fee\u6539\u3002<\/li>\n<li>\u6700\u540e <code>keepLastAssistants<\/code> \u6761\u52a9\u624b\u6d88\u606f\u53d7\u4fdd\u62a4\uff1b\u8be5\u622a\u6b62\u70b9\u4e4b\u540e\u7684\u5de5\u5177\u7ed3\u679c\u4e0d\u4f1a\u88ab\u526a\u679d\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u8db3\u591f\u7684\u52a9\u624b\u6d88\u606f\u6765\u786e\u5b9a\u622a\u6b62\u70b9\uff0c\u5219\u8df3\u8fc7\u526a\u679d\u3002<\/li>\n<li>\u5305\u542b<strong>\u56fe\u50cf\u5757<\/strong>\u7684\u5de5\u5177\u7ed3\u679c\u4f1a\u88ab\u8df3\u8fc7\uff08\u6c38\u4e0d\u4fee\u526a\/\u6e05\u9664\uff09\u3002<\/li>\n<\/ul>\n<h2>\u4e0a\u4e0b\u6587\u7a97\u53e3\u4f30\u7b97<\/h2>\n<p>\u526a\u679d\u4f7f\u7528\u4f30\u7b97\u7684\u4e0a\u4e0b\u6587\u7a97\u53e3\uff08\u5b57\u7b26 \u2248 token \u00d7 4\uff09\u3002\u57fa\u7840\u7a97\u53e3\u6309\u4ee5\u4e0b\u987a\u5e8f\u89e3\u6790\uff1a<\/p>\n<ol>\n<li><code>models.providers.*.models[].contextWindow<\/code> \u8986\u76d6\u3002<\/li>\n<li>\u6a21\u578b\u5b9a\u4e49 <code>contextWindow<\/code>\uff08\u6765\u81ea\u6a21\u578b\u6ce8\u518c\u8868\uff09\u3002<\/li>\n<li>\u9ed8\u8ba4 <code>200000<\/code> token\u3002<\/li>\n<\/ol>\n<p>\u5982\u679c\u8bbe\u7f6e\u4e86 <code>agents.defaults.contextTokens<\/code>\uff0c\u5b83\u5c06\u88ab\u89c6\u4e3a\u89e3\u6790\u7a97\u53e3\u7684\u4e0a\u9650\uff08\u6700\u5c0f\u503c\uff09\u3002<\/p>\n<h2>\u6a21\u5f0f<\/h2>\n<h3>cache-ttl<\/h3>\n<ul>\n<li>\u4ec5\u5f53\u6700\u540e\u4e00\u6b21 Anthropic \u8c03\u7528\u65e9\u4e8e <code>ttl<\/code>\uff08\u9ed8\u8ba4 <code>5m<\/code>\uff09\u65f6\u624d\u8fd0\u884c\u526a\u679d\u3002<\/li>\n<li>\u8fd0\u884c\u65f6\uff1a\u4e0e\u4e4b\u524d\u76f8\u540c\u7684\u8f6f\u4fee\u526a + \u786c\u6e05\u9664\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<h2>\u8f6f\u526a\u679d vs \u786c\u526a\u679d<\/h2>\n<ul>\n<li><strong>\u8f6f\u4fee\u526a<\/strong>\uff1a\u4ec5\u7528\u4e8e\u8fc7\u5927\u7684\u5de5\u5177\u7ed3\u679c\u3002<\/li>\n<li>\u4fdd\u7559\u5934\u90e8 + \u5c3e\u90e8\uff0c\u63d2\u5165 <code>...<\/code>\uff0c\u5e76\u9644\u52a0\u4e00\u4e2a\u5305\u542b\u539f\u59cb\u5927\u5c0f\u7684\u6ce8\u91ca\u3002<\/li>\n<li>\u8df3\u8fc7\u5305\u542b\u56fe\u50cf\u5757\u7684\u7ed3\u679c\u3002<\/li>\n<li><strong>\u786c\u6e05\u9664<\/strong>\uff1a\u7528 <code>hardClear.placeholder<\/code> \u66ff\u6362\u6574\u4e2a\u5de5\u5177\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<h2>\u5de5\u5177\u9009\u62e9<\/h2>\n<ul>\n<li><code>tools.allow<\/code> \/ <code>tools.deny<\/code> \u652f\u6301 <code>*<\/code> \u901a\u914d\u7b26\u3002<\/li>\n<li>\u62d2\u7edd\u4f18\u5148\u3002<\/li>\n<li>\u5339\u914d\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002<\/li>\n<li>\u5141\u8bb8\u5217\u8868\u4e3a\u7a7a =&gt; \u5141\u8bb8\u6240\u6709\u5de5\u5177\u3002<\/li>\n<\/ul>\n<h2>\u4e0e\u5176\u4ed6\u9650\u5236\u7684\u4ea4\u4e92<\/h2>\n<ul>\n<li>\u5185\u7f6e\u5de5\u5177\u5df2\u7ecf\u622a\u65ad\u81ea\u5df1\u7684\u8f93\u51fa\uff1b\u4f1a\u8bdd\u526a\u679d\u662f\u4e00\u4e2a\u989d\u5916\u7684\u5c42\uff0c\u9632\u6b62\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u804a\u5929\u5728\u6a21\u578b\u4e0a\u4e0b\u6587\u4e2d\u7d2f\u79ef\u8fc7\u591a\u7684\u5de5\u5177\u8f93\u51fa\u3002<\/li>\n<li>\u538b\u7f29\u662f\u72ec\u7acb\u7684\uff1a\u538b\u7f29\u8fdb\u884c\u603b\u7ed3\u5e76\u6301\u4e45\u5316\uff0c\u526a\u679d\u662f\u6bcf\u4e2a\u8bf7\u6c42\u7684\u4e34\u65f6\u64cd\u4f5c\u3002\u53c2\u9605 <a href=\"\/concepts\/compaction\">\/concepts\/compaction<\/a>\u3002<\/li>\n<\/ul>\n<h2>\u9ed8\u8ba4\u503c\uff08\u542f\u7528\u65f6\uff09<\/h2>\n<ul>\n<li><code>ttl<\/code>\uff1a<code>\"5m\"<\/code><\/li>\n<li><code>keepLastAssistants<\/code>\uff1a<code>3<\/code><\/li>\n<li><code>softTrimRatio<\/code>\uff1a<code>0.3<\/code><\/li>\n<li><code>hardClearRatio<\/code>\uff1a<code>0.5<\/code><\/li>\n<li><code>minPrunableToolChars<\/code>\uff1a<code>50000<\/code><\/li>\n<li><code>softTrim<\/code>\uff1a<code>{ maxChars: 4000, headChars: 1500, tailChars: 1500 }<\/code><\/li>\n<li><code>hardClear<\/code>\uff1a<code>{ enabled: true, placeholder: \"[Old tool result content cleared]\" }<\/code><\/li>\n<\/ul>\n<h2>\u793a\u4f8b<\/h2>\n<p>\u9ed8\u8ba4\uff08\u5173\u95ed\uff09\uff1a<\/p>\n<p>&#8220;`json5  theme={&#8220;theme&#8221;:{&#8220;light&#8221;:&#8221;min-light&#8221;,&#8221;dark&#8221;:&#8221;min-dark&#8221;}}<br \/>\n{<br \/>\n  agent: {<br \/>\n    contextPruning: { mode: &#8220;off&#8221; },<br \/>\n  },<br \/>\n}<\/p>\n<pre><code>\n\u542f\u7528 TTL \u611f\u77e5\u526a\u679d\uff1a\n\n```json5  theme={&quot;theme&quot;:{&quot;light&quot;:&quot;min-light&quot;,&quot;dark&quot;:&quot;min-dark&quot;}}\n{\n  agent: {\n    contextPruning: { mode: &quot;cache-ttl&quot;, ttl: &quot;5m&quot; },\n  },\n}\n<\/code><\/pre>\n<p>\u9650\u5236\u526a\u679d\u5230\u7279\u5b9a\u5de5\u5177\uff1a<\/p>\n<p><code>json5  theme={\"theme\":{\"light\":\"min-light\",\"dark\":\"min-dark\"}}<br \/>\n{<br \/>\n  agent: {<br \/>\n    contextPruning: {<br \/>\n      mode: \"cache-ttl\",<br \/>\n      tools: { allow: [\"exec\", \"read\"], deny: [\"*image*\"] },<br \/>\n    },<br \/>\n  },<br \/>\n}<\/code><\/p>\n<p>\u53c2\u9605\u914d\u7f6e\u53c2\u8003\uff1a<a href=\"\/gateway\/configuration\">Gateway \u7f51\u5173\u914d\u7f6e<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Session pruning \u4f1a\u8bdd\u526a\u679d \u4f1a\u8bdd\u526a\u679d\u5728\u6bcf\u6b21 LLM \u8c03\u7528\u4e4b\u524d\u4ece\u5185\u5b58\u4e0a\u4e0b\u6587\u4e2d\u4fee\u526a\u65e7\u7684\u5de5\u5177\u7ed3\u679c\u3002\u5b83\u4e0d [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-docs"],"_links":{"self":[{"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":1,"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":399,"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/posts\/104\/revisions\/399"}],"wp:attachment":[{"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pa.yingzhi8.cn\/index.php\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}