context-sweet-spot
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFind the Context Size Where Your Sessions Stop Paying
找出上下文达到何种规模后会让会话不再划算
This skill answers one question with the user's own data: at what context size do their coding-agent sessions become a bad deal? Long context is not free even when it fits the window: every cache rebuild re-bills the whole context at write rates, compactions burn turns, and models degrade before their window ends. The sweet spot is where those costs start outrunning the value of the carried context. It is read-only on the platform. Locally it writes a trace export while it works and deletes it again, and leaves one report file behind.
此 skill 使用用户自己的数据回答一个问题:用户的 coding-agent 会话在多大上下文时不再划算?长上下文即使仍能装入 window 也不是免费的:每次 cache rebuild 都会按 cache-write 费率对整个上下文重新计费,compaction 会消耗对话轮次,而模型甚至在 window 耗尽前就开始退化。最优位置就是这些成本开始超过所携带上下文价值的位置。此 skill 在平台上的操作是只读的。在本地工作时,它会写入一个 trace export,完成后再将其删除,并且只留下一个报告文件。
Step 1: Set up the LangWatch CLI
第 1 步:设置 LangWatch CLI
Use the CLI for everything: documentation (, ) and platform operations (prompts, scenarios, evaluators, datasets, monitors, traces, analytics). Install it once with , then run the binary directly; an unpinned re-resolves the package from the registry on every run.
langwatchlangwatch docs ...langwatch scenario-docs ...npm install -g langwatchlangwatchnpx langwatchCoding-agent sessions live in the user's personal LangWatch workspace by default. signs this machine in; add on the read commands when the sessions live in a team project instead.
langwatch login --device--project <slug>Use to read documentation as Markdown. Some useful entry points:
langwatch docs <path>bash
langwatch docs # Docs index
langwatch docs integration/python/guide # Python integration
langwatch docs integration/typescript/guide # TypeScript integration
langwatch docs prompt-management/cli # Prompts CLI
langwatch scenario-docs # Scenario docs indexDiscover commands with and . List and get commands accept for machine-readable output. Every list command takes to cap the rows and to read part of the answer. A paginated list answers with an envelope, so count its rows through the row array (), and read how many there are in all at . Bare counts the fields of the envelope, not the rows. Read the docs first instead of guessing SDK APIs or CLI flags.
langwatch --helplangwatch <subcommand> --help--format json--limit <n>--jq <expr>--jq '.traces | length'.pagination.total--jq lengthIf no shell is available, fetch the same Markdown over plain HTTP. Append to any docs path (e.g. https://langwatch.ai/docs/integration/python/guide.md). Index: https://langwatch.ai/docs/llms.txt. Scenario index: https://langwatch.ai/scenario/llms.txt
.mdIf anything fails or confuses you while following this skill (broken commands, docs that do not match reality, errors you had to work around), ask the user for permission and run with a and (or ) to send it to the LangWatch team, and it directly shapes what gets fixed. No login or API key needed. Nothing is sent without , and prints the exact payload without sending anything. The title, summary and transcript are scrubbed locally first, by pattern: secrets and API keys, plus email addresses, phone numbers, card numbers and public IPv4 addresses. Anything no pattern matches is sent as written, including a contact address passed with . With , always run first and let the user read the payload, because a transcript carries content they never reviewed. explains the options.
npx --yes langwatch report --user-approved--title--summary--session <transcript.jsonl>--user-approved--dry-run--email--session--dry-runnpx --yes langwatch report --help所有操作都使用 CLI:文档(、)和平台操作(prompts、scenarios、evaluators、datasets、monitors、traces、analytics)。先安装一次 ,然后直接运行 可执行文件;未固定版本的 每次运行都会从 registry 重新解析 package。
langwatchlangwatch docs ...langwatch scenario-docs ...npm install -g langwatchlangwatchnpx langwatch默认情况下,coding-agent 会话位于用户的个人 LangWatch workspace 中。 可让当前机器登录;如果会话位于团队项目中,则在读取命令中添加 。
langwatch login --device--project <slug>使用 以 Markdown 格式阅读文档。一些实用入口如下:
langwatch docs <path>bash
langwatch docs # Docs index
langwatch docs integration/python/guide # Python integration
langwatch docs integration/typescript/guide # TypeScript integration
langwatch docs prompt-management/cli # Prompts CLI
langwatch scenario-docs # Scenario docs index通过 和 发现命令。list 和 get 命令接受 ,以输出机器可读结果。每个 list 命令都可使用 限制返回行数,使用 读取答案的一部分。分页 list 返回一个 envelope,因此需要通过 row 数组统计行数(),并通过 读取总数。单独使用 统计的是 envelope 的字段数,而不是行数。先阅读文档,不要猜测 SDK API 或 CLI flag。
langwatch --helplangwatch <subcommand> --help--format json--limit <n>--jq <expr>--jq '.traces | length'.pagination.total--jq length如果环境没有 shell,可以通过普通 HTTP 获取相同的 Markdown。为任意 docs 路径追加 (例如 https://langwatch.ai/docs/integration/python/guide.md)。索引:https://langwatch.ai/docs/llms.txt。Scenario 索引:https://langwatch.ai/scenario/llms.txt
.md如果执行此 skill 期间有任何失败或令人困惑之处(命令失效、文档与实际情况不符、你不得不绕过错误等),请先征得用户许可,然后运行 ,并附带 和 (或 ),将反馈发送给 LangWatch 团队;这些反馈会直接影响后续修复内容。无需登录或 API key。没有 就不会发送任何内容,而 会打印准确的 payload 但不发送。title、summary 和 transcript 会先在本地按模式脱敏,包括 secrets、API keys、email addresses、phone numbers、card numbers 和 public IPv4 addresses。任何未被模式匹配的内容都会原样发送,包括通过 传入的联系地址。使用 时,务必先运行 并让用户阅读 payload,因为 transcript 可能包含他们从未审阅过的内容。 会说明各选项。
npx --yes langwatch report --user-approved--title--summary--session <transcript.jsonl>--user-approved--dry-run--email--session--dry-runnpx --yes langwatch report --helpStep 2: Collect the Sessions
第 2 步:收集会话
Pick one window and use it everywhere, because defaults to the last 7 days. Compute a start and an end date once (30 days back to now is a good default) and pass both:
trace exportbash
langwatch trace export --origin coding_agent --format jsonl --limit 20000 \
--start-date <start> --end-date <end> -o coding-traces.jsonl--limitExported 20000 traces (48213 total)--limitReport the window you used in the report, and delete once the analysis is done.
coding-traces.jsonlEach trace carries (the session id) and (which agent). Collect the distinct session ids, then for each session with enough turns to mean anything (5 or more model calls):
metadata.thread_idmetadata."langwatch.source"bash
langwatch session events <sessionId> --format jsonThe events are the raw material: every model call with its input, output, cache-read and cache-creation tokens, its cost, its model, plus explicit and events.
compactionrate_limit选定一个时间窗口,并在所有位置统一使用,因为 默认只导出最近 7 天的数据。一次性计算开始和结束日期(默认可取从现在往前 30 天),并同时传入两者:
trace exportbash
langwatch trace export --origin coding_agent --format jsonl --limit 20000 \
--start-date <start> --end-date <end> -o coding-traces.jsonl--limitExported 20000 traces (48213 total)--limit在报告中写明所用的时间窗口,并在分析完成后删除 。
coding-traces.jsonl每条 trace 都包含 (session id)和 (所用 agent)。收集所有不同的 session id,然后针对每个轮次足够、具备分析意义的会话(至少 5 次 model call):
metadata.thread_idmetadata."langwatch.source"bash
langwatch session events <sessionId> --format json这些 events 是原始素材:包含每次 model call 的 input、output、cache-read 和 cache-creation tokens、cost、model,以及明确的 和 events。
compactionrate_limitStep 3: Compute the Economics
第 3 步:计算经济性
Write a small local script (python3 or jq) over the events. Per session, compute:
- Peak context: the largest (input + cache-read) of any model call, and its share of the model's context window.
- Cache rebuilds: model calls whose cache-creation tokens are the bulk of their input, counted only once the session already holds cached context. The first cache-creating call of a session builds the cache rather than rebuilding it, and it pays for the context once, so it is a setup cost and not a rebuild. Count a write from the first call that follows a call with cache-read tokens, or that follows an explicit cache miss. Each rebuild re-paid for context that was already paid for, at the provider's cache-write rate. That rate is specific to the provider and the model: some price a write above fresh input, some price it the same, and some charge for cache storage by time instead. Take it from the price card of the model in question, never from a rule of thumb.
- Compaction count and where they landed: a compaction late in a session marks the point where the carried context stopped fitting.
- Cost per model call over session lifetime: split each session into thirds by call order and compare the average cost per call between the first and last third.
- Waiting time around rebuilds: rebuilt context is also re-uploaded and re-processed, so rebuild-heavy sessions are slower per turn.
Then aggregate across sessions: bucket by peak-context share (for example under 25%, 25 to 50%, 50 to 75%, over 75% of the window) and compare cost per call, rebuild rate and compaction rate between buckets. The sweet spot is the highest bucket where those three stay flat.
编写一个小型本地脚本(python3 或 jq)处理 events。按 session 计算:
- Peak context(峰值上下文):任一 model call 的最大(input + cache-read),以及它占 model context window 的比例。
- Cache rebuild(缓存重建):对于 cache-creation tokens 占 input 大部分的 model call,仅在 session 已经持有缓存上下文后才计数。session 中首次创建缓存的调用是在建立 cache,而不是 rebuild;它只支付一次上下文费用,因此属于 setup cost,不应算作 rebuild。如果一次 cache write 紧跟在带 cache-read tokens 的调用之后,或紧跟在明确的 cache miss 之后,则将其计为一次 rebuild。每次 rebuild 都会按 provider 的 cache-write rate,再次为已经付费的上下文计费。该费率因 provider 和 model 而异:有些 model 的 write 价格高于 fresh input,有些与之相同,有些则按时间收取 cache storage 费用。必须从相应 model 的 price card 获取该费率,绝不能套用经验法则。
- Compaction 次数及发生位置:session 后期出现 compaction,标志着所携带的上下文已经不再装得下。
- Session 生命周期内每次 model call 的成本:按调用顺序将每个 session 分为三等份,并比较第一等份与最后等份的平均每次调用成本。
- Rebuild 前后的等待时间:重建后的上下文还要重新上传和重新处理,因此 rebuild 较多的 session 每轮也会更慢。
然后跨 session 汇总:按 peak-context 占比分桶(例如 window 的 25% 以下、25% 至 50%、50% 至 75%、75% 以上),并比较各桶的 cost per call、rebuild rate 和 compaction rate。最优分桶就是这三项指标仍保持平稳的最高分桶。
Step 4: Report the Finding
第 4 步:报告结论
Write a single self-contained in the project root (inline CSS, no external assets) with:
context-sweet-spot-report.html- The number: the context share where this user's sessions start degrading, stated in the first line ("your sessions stay economical up to about 55% of the window; past that, cost per turn doubles")
- The bucket comparison table with cost per call, rebuild rate and compaction rate per bucket
- The three most expensive sessions dissected: where the context grew, where it rebuilt, what one rebuild cost
- Concrete habits, each tied to the evidence: when to start a fresh session instead of pushing through, what to offload to sub-agents (the sub-agent keeps its context out of the main session), whether the user's compactions happen late, after the context already crossed the sweet spot
- Links to example sessions in LangWatch for every claim
Also state the top finding directly in the conversation, leading with the number. The LangWatch session detail shows the same cache-health stats per session (), so name it as the place to watch the habit change.
/me/sessions在项目根目录编写一个自包含的 (使用 inline CSS,不引用外部资源),内容包括:
context-sweet-spot-report.html- 核心数字:用户 session 开始退化的上下文占比,并在第一行直接说明(“你的 session 在不超过 window 约 55% 时仍然划算;超过后,每轮成本会翻倍”)
- 各分桶对比表,包括每个分桶的 cost per call、rebuild rate 和 compaction rate
- 对三个成本最高的 session 逐一拆解:上下文在何处增长、在何处 rebuild,以及一次 rebuild 花费了多少
- 具体习惯,每项都有证据支撑:何时应该开启新 session 而不是硬撑;哪些内容应 offload 给 sub-agents(sub-agent 可让自身上下文不进入主 session);用户的 compaction 是否发生得太晚,即在上下文已经越过最优位置之后才发生
- 为每项结论提供 LangWatch 中示例 session 的链接
还要在对话中直接说明首要结论,并以该数字开头。LangWatch 的 session detail 页面会显示每个 session 的同等 cache-health 统计信息(),因此应指出这里是观察习惯变化的位置。
/me/sessionsCommon Mistakes
常见错误
- Do NOT judge context by peak share alone; a fat context that never rebuilds is cheap, and a modest one that rebuilds every turn is expensive. The rebuild rate carries the finding.
- Do NOT compare sessions across different models as one population; window sizes and cache pricing differ. Bucket per model, then compare.
- Do NOT count cache-read tokens as cost the way input tokens are; they bill at a fraction. The split is in the event rows, use it.
- Do NOT count the first cache-creating call of a session as a rebuild. It builds the cache, and counting it makes every short session look rebuild-heavy and drags the whole bucket with it.
- Do NOT report a threshold without the sessions behind it; every bucket statistic needs 2 or 3 example session ids.
- Do NOT include sessions with fewer than 5 model calls; they carry no lifetime signal and flatten the buckets.
- If the CLI returns an error, report the user-facing consequence, not the raw error text.
- 不要仅凭 peak share 判断上下文;从不 rebuild 的臃肿上下文成本很低,而每轮都 rebuild 的适中上下文成本很高。rebuild rate 才是决定结论的关键。
- 不要把不同 model 的 session 当作同一个样本总体比较;它们的 window size 和 cache pricing 不同。应按 model 分桶后再比较。
- 不要按 input token 的方式计算 cache-read tokens 的成本;它们按较小比例计费。event rows 中已提供拆分,应直接使用。
- 不要把 session 首次创建 cache 的调用计为 rebuild。它只是在建立 cache;若计入,会让每个短 session 看起来 rebuild 过多,并把整个分桶拉偏。
- 不要在没有 session 支撑的情况下报告 threshold;每个分桶统计都需要 2 或 3 个示例 session id。
- 不要纳入 model call 少于 5 次的 session;它们无法提供生命周期信号,并会压平分桶差异。
- 如果 CLI 返回错误,应报告对用户造成的影响,而不是原始错误文本。