Agent Skillslangwatch/langwatch › context-sweet-spot

context-sweet-spot

GitHub

分析 LangWatch 编码代理会话数据,通过评估缓存重建、压缩及成本等指标,确定上下文大小的最佳经济平衡点,生成优化报告以控制成本并提升效率。

skills/_compiled/native/context-sweet-spot/SKILL.md langwatch/langwatch

Trigger Scenarios

需要优化编码代理会话的上下文大小 感觉编码代理会话成本过高或性能随上下文增长而下降

Install

npx skills add langwatch/langwatch --skill context-sweet-spot -g -y
More Options

Non-standard path

npx skills add https://github.com/langwatch/langwatch/tree/main/skills/_compiled/native/context-sweet-spot -g -y

Use without installing

npx skills use langwatch/langwatch@context-sweet-spot

指定 Agent (Claude Code)

npx skills add langwatch/langwatch --skill context-sweet-spot -a claude-code -g -y

安装 repo 全部 skill

npx skills add langwatch/langwatch --all -g -y

预览 repo 内 skill

npx skills add langwatch/langwatch --list

SKILL.md

Frontmatter
{
    "name": "context-sweet-spot",
    "license": "MIT",
    "description": "Investigates the context economics of your own coding-agent sessions in LangWatch. Reads real sessions to find where carrying a fat context stops paying for itself, measured in cache rebuilds, compactions and cost per turn, and delivers a report with the context size your sessions should stay under, with example sessions behind every claim. Use when coding-agent sessions feel expensive or degrade as they grow.",
    "user-prompt": "Find my context sweet spot",
    "compatibility": "Works with Claude Code and similar AI assistants. The `langwatch` CLI is the only interface."
}

Find 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.

Step 1: Set up the LangWatch CLI

Use the langwatch CLI for everything: documentation (langwatch docs ..., langwatch scenario-docs ...) and platform operations (prompts, scenarios, evaluators, datasets, monitors, traces, analytics). Install it once with npm install -g langwatch, then run the langwatch binary directly; an unpinned npx langwatch re-resolves the package from the registry on every run.

Coding-agent sessions live in the user's personal LangWatch workspace by default. langwatch login --device signs this machine in; add --project <slug> on the read commands when the sessions live in a team project instead.

Step 2: Collect the Sessions

Pick one window and use it everywhere, because trace export 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:

langwatch trace export --origin coding_agent --format jsonl --limit 20000 \
  --start-date <start> --end-date <end> -o coding-traces.jsonl

--limit caps the whole export, not one page, so a window with more matches than the limit gives a partial file and the sessions it drops are the ones missing from the buckets. The command reports both counts when it truncates, for example Exported 20000 traces (48213 total). Raise --limit until the two agree, or say in the report that the buckets come from a sample of N of M traces.

Report the window you used in the report, and delete coding-traces.jsonl once the analysis is done.

Each trace carries metadata.thread_id (the session id) and metadata."langwatch.source" (which agent). Collect the distinct session ids, then for each session with enough turns to mean anything (5 or more model calls):

langwatch session events <sessionId> --format json

The events are the raw material: every model call with its input, output, cache-read and cache-creation tokens, its cost, its model, plus explicit compaction and rate_limit events.

Step 3: Compute the Economics

Write a small local script (python3 or jq) over the events. Per session, compute:

  1. Peak context: the largest (input + cache-read) of any model call, and its share of the model's context window.
  2. 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.
  3. Compaction count and where they landed: a compaction late in a session marks the point where the carried context stopped fitting.
  4. 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.
  5. 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.

Step 4: Report the Finding

Write a single self-contained context-sweet-spot-report.html in the project root (inline CSS, no external assets) with:

  • 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 (/me/sessions), so name it as the place to watch the habit change.

Common 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.

Version History

  • 6f9d4a4 Current 2026-08-28 21:09

Same Skill Collection

.claude/skills/browser-pair/SKILL.md
.claude/skills/browser-test/SKILL.md
.claude/skills/code-review/SKILL.md
.claude/skills/feature-map/SKILL.md
.claude/skills/haven-setup/SKILL.md
.claude/skills/langwatch-kanban/SKILL.md
plugins/langwatch/skills/langwatch/SKILL.md
services/langy-agent/skills/github/SKILL.md
skills/_compiled/native/agent-best-practices/SKILL.md
skills/_compiled/native/agent-performance/SKILL.md
skills/_compiled/native/connect-agent/SKILL.md
skills/_compiled/native/datasets/SKILL.md
skills/_compiled/native/debug-instrumentation/SKILL.md
skills/_compiled/native/debug-with-langwatch/SKILL.md
skills/_compiled/native/drive-the-ui/SKILL.md
skills/_compiled/native/eval-triage/SKILL.md
skills/_compiled/native/evaluate-multimodal/SKILL.md
skills/_compiled/native/evaluations/SKILL.md
skills/_compiled/native/experiments/SKILL.md
skills/_compiled/native/generate-rag-dataset/SKILL.md
skills/_compiled/native/github/SKILL.md
skills/_compiled/native/level-up/SKILL.md
skills/_compiled/native/lwql-charts/SKILL.md
skills/_compiled/native/online-evaluations/SKILL.md
skills/_compiled/native/prompt-optimization/SKILL.md
skills/_compiled/native/prompts/SKILL.md
skills/_compiled/native/provider-cost-comparison/SKILL.md
skills/_compiled/native/scenarios/SKILL.md
skills/_compiled/native/setup-lw/SKILL.md
skills/_compiled/native/test-cli-usability/SKILL.md
skills/_compiled/native/test-compliance/SKILL.md
skills/_compiled/native/tracing/SKILL.md

Metadata

Files
0
Version
6f9d4a4
Hash
21119ee6
Indexed
2026-08-28 21:09

Accueil - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-29 05:55
浙ICP备14020137号-1 $Carte des visiteurs$