langchain-cost-tuning

Featured

Optimize LangChain API costs with token tracking, model tiering, caching, prompt compression, and budget enforcement. Trigger: "langchain cost", "langchain tokens", "reduce langchain cost", "langchain billing", "langchain budget", "token optimization".

AI & Automation 2,944 stars 440 forks Updated today MIT

Install

View on GitHub

Quality Score: 99/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# LangChain Cost Tuning ## Overview Reduce LLM API costs while maintaining quality: token tracking callbacks, model tiering (route simple tasks to cheap models), caching for duplicate queries, prompt compression, and budget enforcement. ## Current Pricing Reference (2026) | Provider | Model | Input $/1M | Output $/1M | |----------|-------|-----------|------------| | OpenAI | gpt-4o | $2.50 | $10.00 | | OpenAI | gpt-4o-mini | $0.15 | $0.60 | | Anthropic | claude-sonnet | $3.00 | $15.00 | | Anthropic | claude-haiku | $0.25 | $1.25 | | OpenAI | text-embedding-3-small | $0.02 | - | ## Strategy 1: Token Usage Tracking ```typescript import { BaseCallbackHandler } from "@langchain/core/callbacks/base"; const MODEL_PRICING: Record<string, { input: number; output: number }> = { "gpt-4o": { input: 2.5, output: 10.0 }, "gpt-4o-mini": { input: 0.15, output: 0.6 }, }; class CostTracker extends BaseCallbackHandler { name = "CostTracker"; totalCost = 0; totalTokens = 0; calls = 0; handleLLMEnd(output: any) { this.calls++; const usage = output.llmOutput?.tokenUsage; if (!usage) return; const model = "gpt-4o-mini"; // extract from output metadata const pricing = MODEL_PRICING[model] ?? MODEL_PRICING["gpt-4o-mini"]; const inputCost = (usage.promptTokens / 1_000_000) * pricing.input; const outputCost = (usage.completionTokens / 1_000_000) * pricing.output; this.totalTokens += usage.totalTokens; this.totalCost += inputCost + outputCos...

Details

Author
jeremylongshore
Repository
jeremylongshore/claude-code-plugins-plus-skills
Created
1 years ago
Last Updated
today
Language
Python
License
MIT

Integrates with

Bundled in these plugins

Similar Skills

Semantically similar based on skill content — not just same category