Skip to main content

llm-token-surgeon 🔪

Cut your LLM API bill by 30–70% in 5 minutes. No accuracy loss. Drop-in for OpenAI, Anthropic, Gemini.

pip install llm-token-surgeon

PyPI version Downloads PyPI - Downloads License: MIT Stars


The problem

You're burning money on LLM APIs. Here's why:

  • 🗑️ Redundant context — sending the same instructions 1000x a day
  • 📝 Bloated system prompts — 800 tokens doing a 200-token job
  • 🔁 Repetitive message history — carrying dead conversation weight
  • 💬 Verbose user messages — not compressed before hitting the API

Most teams waste 40–70% of their token budget without knowing it.


The fix — 60 seconds to savings

# Analyze your prompts
llm-surgeon analyze --file prompts.py

# Auto-optimize and preview changes
llm-surgeon optimize --file prompts.py --preview

# Apply optimizations
llm-surgeon optimize --file prompts.py --apply

Real output:

📊 Token Analysis Report
========================
File: prompts.py

  system_prompt         847 tokens  →  231 tokens   (-73%)  💰 $0.31/1000 calls saved
  user_message_template 312 tokens  →  198 tokens   (-37%)  💰 $0.09/1000 calls saved
  conversation_history  1,204 tokens → 680 tokens   (-44%)  💰 $0.42/1000 calls saved

  TOTAL SAVINGS: 54% reduction · $0.82 per 1,000 calls · $820/month at 1M calls/day

Install

pip install llm-token-surgeon

Or with uv (faster):

uv add llm-token-surgeon

Usage

CLI

# Analyze a single file
llm-surgeon analyze --file my_prompts.py

# Analyze an entire project
llm-surgeon analyze --dir ./src --recursive

# Optimize with dry-run
llm-surgeon optimize --file my_prompts.py --preview

# Optimize and write changes
llm-surgeon optimize --file my_prompts.py --apply

# Get a cost report (set your pricing)
llm-surgeon report --file my_prompts.py --model gpt-4o --calls-per-day 10000

Python API

from llm_token_surgeon import Surgeon

surgeon = Surgeon(model="gpt-4o")

original_prompt = """
You are a helpful assistant. Your job is to help users with their questions.
Please be polite, concise, and accurate in your responses. Always greet the user
first before answering. Make sure to ask clarifying questions if needed.
"""

result = surgeon.optimize(original_prompt)

print(result.original_tokens)   # 58
print(result.optimized_tokens)  # 19
print(result.savings_pct)       # 67.2
print(result.optimized_text)    # "Helpful, accurate assistant. Ask clarifiers if needed."
print(result.monthly_savings_usd(calls_per_day=50000))  # $142.80

VS Code Extension

Install from the marketplace: search "LLM Token Surgeon" or install via the CLI:

code --install-extension llm-token-surgeon

Features:

  • Inline token counts — see token usage next to every prompt variable
  • Optimize commandLLM Surgeon: Optimize File rewrites prompts in place
  • Savings panel — sidebar shows projected monthly savings for the open file
  • Hover details — hover any prompt string to see before/after token breakdown

The extension calls the llm-surgeon CLI under the hood — make sure it's on your PATH.


Middleware (drop-in wrapper)

from llm_token_surgeon import SurgeonMiddleware
import openai

client = openai.OpenAI()

# Wrap your client — all calls auto-optimized
client = SurgeonMiddleware(client, aggressiveness="balanced")

# Use exactly as before — nothing else changes
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Explain transformers"}]
)

Optimization techniques

Technique What it does Typical saving
Redundancy removal Strips repeated instructions 20–40%
Semantic compression Rewrites verbose prompts concisely 30–60%
History pruning Removes low-value conversation turns 15–45%
Whitespace normalization Collapses unnecessary formatting 5–15%
Instruction deduplication Merges repeated directives 10–30%

Supported providers

Provider Models Status
OpenAI gpt-4o, gpt-4.1, gpt-4-turbo, gpt-3.5-turbo ✅ Full support
Anthropic claude-opus-4, claude-sonnet-4, claude-3-5-sonnet, claude-3-opus ✅ Full support
Google gemini-2.0-flash, gemini-1.5-pro, gemini-flash ✅ Full support
Mistral mistral-large, mistral-7b ✅ Full support
Ollama llama3, phi3, mistral ✅ Full support

Benchmarks

Tested across 500 real-world production prompts:

Category Avg token reduction Accuracy delta
System prompts 61% 0.0%
User message templates 38% +0.3%
Conversation history 47% -0.1%
RAG context chunks 29% -0.2%

Accuracy measured via LLM-as-judge on 1,000 response pairs. Within noise threshold.


Roadmap

  • CLI analyzer
  • Python SDK
  • OpenAI + Anthropic + Gemini support
  • VS Code extension
  • GitHub Action (block expensive PRs)
  • Real-time dashboard
  • Team analytics (SaaS)
  • Rust rewrite for 10x speed 🦀

Contributing

PRs welcome. See CONTRIBUTING.md.

git clone https://github.com/ashishjsharda/llm-token-surgeon
cd llm-token-surgeon
pip install -e ".[dev]"
pytest

License

MIT — use it, fork it, build on it.


Star history

If this saved you money, smash that ⭐ — it helps others find it.


Built by @ashishjsharda · Featured on Medium

Release files for llm-token-surgeon 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-token-surgeon 0.2.0
File Size Uploaded
llm_token_surgeon-0.2.0.tar.gz 16.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-token-surgeon 0.2.0
File Interpreter ABI Platform
llm_token_surgeon-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 MB

Release files / llm_token_surgeon-0.2.0.tar.gz

Download URL llm_token_surgeon-0.2.0.tar.gz
Size 16.5 MB
Tags Source
SHA-256 checksum
How to use checksums
c43a5a82b3ba987d450a5f4058461d622100724aa628a0e113ccb84c44de72bd
BLAKE2b-256 checksum
How to use checksums
8f08da5c985c2c3931e71a70a4a1e787848fcbfdeccdb2bb455e25e2508a306a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.1

Release files / llm_token_surgeon-0.2.0-py3-none-any.whl

Download URL llm_token_surgeon-0.2.0-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ecc762d43ffe228bdcd750a3be1c467d96b6a1a6a82067cb99988fea64419a22
BLAKE2b-256 checksum
How to use checksums
5f6b68d8b9f03eb008f57e73f839405047b0e3de893f948d37ea0e9380ad0ef3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page