Agent skill · AI & Agents

langchain-cost-tuning

Control LangChain 1.0 AI spend with accurate streaming token accounting, model tiering, provider-specific cache hit tuning, per-tenant budgets, and retry dedup. Use when AI spend grows faster than traffic, a cost regression lands, or you need per-tenant budget enforcement. Trigger with "langchain cost", "langchain token accounting", "langchain per-tenant budget", "langchain model tiering", "prompt cache savings". '

jeremylongshoregithub.com/jeremylongshoreGitHub ↗
claude-codecan modify filesMIT
Install
npx skills add jeremylongshore/claude-code-plugins-plus-skills --skill langchain-cost-tuning --agent claude-code

Same command for any agent — swap --agent for codex, cursor, copilot.

Facts
Files in the skill folder: 6
SKILL.md size: 19 KB
Bundled scripts: none
Version: 2.5.0
Declared author: Jeremy Longshore <jeremy@intentsolutions.io>
Allowed tools: ReadWriteEditBash(python:*)Bash(redis-cli:*)
Requires: Designed for Claude Code, also compatible with Codex
Path: skills/.curated/langchain-cost-tuning/SKILL.md
Open the folder on GitHub →
Where it comes from
Stars: 2,596
Language: Python
Read our review of the source →

Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.

Review
written from the skill's own SKILL.md · Aug 5, 2026

What it does

The skill provides a runbook for tuning LangChain 1.0 usage to manage AI spend. It covers multiple levers: token accounting, retry discipline, agent loop caps, caching behavior, and model tiering. It also defines per-tenant budget enforcement, cache-key considerations for tool binding, and calibration of a semantic cache threshold. It includes a concrete per-tenant budget middleware outline and specific steps to implement and validate these controls.

How it works

  • Step 1 reads and reports usage via usage_metadata to ensure streaming-accurate token accounting, avoiding response_metadata tokenUsage alone.
  • Step 2 uses astream_events(version="v2") to stream and aggregate on_chat_model_stream events for precise meters.
  • Step 3 introduces a retry-aware meter that deduplicates emissions by run_id and optionally by a stable request_id, with guidance to place token accounting above retry middleware or to dedupe on the run_id/request_id.
  • Step 4 provides a model-tiering decision framework with a per-1M pricing snapshot for several models, advising when to favor cheaper tiers for drafts vs. more expensive models for final outputs.
  • Step 5 aggregates Anthropic cache usage per session/tenant, calculating savings and persisting a CacheLedger per tenant/day for budget checks.
  • Step 6 enforces tool-aware cache keys to prevent semantically wrong cache hits when bound tools are used, recommending composite keys with tool fingerprints.
  • Step 7 calibrates RedisSemanticCache thresholds (not default 0.95) by collecting labeled pairs and tuning to keep false positives under 2%.
  • Step 8 implements per-tenant budget middleware with soft and hard caps, using Redis to track daily usage and to emit alerts or reject calls when hard caps are reached.
  • Step 9 caps recursion in agent loops (recommended 5-10 for interactive, 10-15 for batch) and suggests using trim_messages with include_system and start_on to control context.

When to use it

Use when AI spend grows faster than traffic, a cost regression lands, or you need per-tenant budget enforcement. Trigger phrases include: "langchain cost", "langchain token accounting", "langchain per-tenant budget", "langchain model tiering", and "prompt cache savings".

What it can touch

  • usage_metadata (token accounting and cache fields)
  • astream_events(version="v2")
  • Retry-aware meter code (with run_id and optional request_id)
  • Redis for per-tenant budget state and soft/hard caps
  • InMemoryCache/SQLiteCache/RedisSemanticCache with tool-aware keys
  • Per-tenant budget middleware class and Redis schema
  • Recursion limit configuration in agent creation

Caveats

  • Requires Claude Code compatibility (also compatible with Codex)
  • Licensing: MIT
  • Calibration steps for cache threshold and model tiering rely on current pricing snapshots and gold-set validation; prices and thresholds are not guaranteed constants.
From the SKILL.md

# LangChain Cost Tuning (Python) ## Overview An engineer shipped a new research agent Tuesday. By Friday the Anthropic bill had grown 6x while traffic grew 1.4x. The cost dashboard — wired to `on_llm_end` — showed spend up maybe 2x. Reconciling against the provider console on Monday surfaced two compounding bugs: (1) the agent's `ChatOpenAI` fallback kept the default `max_retries=6`, so each logic

More from claude-code-plugins-plus-skills
All skills →
About this skill
What does the langchain-cost-tuning skill do?

Control LangChain 1.0 AI spend with accurate streaming token accounting, model tiering, provider-specific cache hit tuning, per-tenant budgets, and retry dedup. Use when AI spend grows faster than traffic, a cost regression lands, or you need per-tenant budget enforcement. Trigger with "langchain cost", "langchain token accounting", "langchain per-tenant budget", "langchain model tiering", "prompt cache savings". '

How do I install it?

Run `npx skills add jeremylongshore/claude-code-plugins-plus-skills --skill langchain-cost-tuning --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.

Where does this skill come from?

From jeremylongshore/claude-code-plugins-plus-skills, a repository with 2,596 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.

Is a popular skill a good skill?

Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.

Keep going