Skip to main content

🛡️ LLM Token-Saver

License: MIT Python 3.8+ Run Tests

LLM Token-Saver 是一个极致轻量、无侵入的 LLM API 中间件。只需一行代码包装你现有的 OpenAI 兼容客户端,就能自动优化 Prompt 结构、最大化上下文缓存(Context Cache)命中率、并压缩超长对话历史,帮你直接砍掉一大笔 API 账单。

✨ 核心特性

  • ⚡ 零侵入接入:完全兼容 OpenAI 官方 SDK 格式,无需重写业务逻辑。
  • 🎯 前缀稳定器 (Prefix Stabilizer):自动把 System Prompt 中的动态变量(时间戳等)挪到末尾,确保请求前缀字节级一致,强制触发缓存命中。
  • 🧩 JSON/Tools 稳定器 (Json Stabilizer):对 tools / functions 定义做递归键排序,即使你每次写代码时字典键序不同,发出去的字符串也字节级一致 —— Tool 定义再大也不会因为键序抖动而反复失效。
  • ✂️ 上下文瘦身 (Context Pruner):超限时自动截断最早的历史,防止输入 Token 无限膨胀。
  • 🧠 语义级裁剪 (Semantic Pruner):可选增强 —— 超限时调用一个便宜模型把早期对话压缩成一句话摘要,多轮对话更连贯,省 Token 更多。
  • 🤐 输出防废话 (Output Constraint):自动注入隐藏提示词,让大模型直接给结果,减少输出 Token 浪费。

📦 安装

pip install -e .

🚀 极简用法

from openai import OpenAI
from token_saver import TokenSaver

raw_client = OpenAI(api_key="your-api-key", base_url="https://api.deepseek.com/v1")
client = TokenSaver(client=raw_client)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "你是一个助手。当前时间:2024-05-20 12:00:00"},
        {"role": "user", "content": "给我讲个笑话"},
    ],
)

🛠️ 高级配置

client = TokenSaver(
    client=raw_client,
    max_history=10,             # 最多保留的消息条数(含 system)
    stabilize_tools=True,       # 是否对 tools 做 JSON 键排序
)

语义级历史裁剪(省钱利器)

传入一个便宜模型的客户端,超限时自动把早期对话总结成一句话:

from openai import OpenAI

raw_client = OpenAI(...)              # 主模型(贵)
cheap_client = OpenAI(...)            # 便宜模型(如 qwen-turbo / gpt-3.5-turbo)

client = TokenSaver(
    client=raw_client,
    use_semantic_pruning=True,
    summarizer_client=cheap_client,
    summary_model="qwen-turbo",
    keep_recent=4,                    # 保留最近 4 条不压缩
)

⚠️ 注意:summarizer_client 必须是一个原生客户端,不要再套一层 TokenSaver,否则会递归总结。

🧪 测试

pip install -e ".[dev]"
pytest tests/ -v

每次 push 到 GitHub 会自动跑一遍测试(见 .github/workflows/test.yml),覆盖 Python 3.8 ~ 3.12。

🤝 贡献与反馈

欢迎提交 Issue 或 Pull Request!如果你觉得这个项目帮你省了钱,请给个 ⭐️ Star 支持一下!

Metadata

Release files for llm-token-saver 0.3.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-saver 0.3.0
File Size Uploaded Provenance Actions
llm_token_saver-0.3.0.tar.gz 12.1 kB Without provenance Details for llm_token_saver-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llm-token-saver 0.3.0
File Interpreter ABI Platform Actions
llm_token_saver-0.3.0-py3-none-any.whl Python 3 none any Details for llm_token_saver-0.3.0-py3-none-any.whl

Total release size: 24.8 kB

Release files / llm_token_saver-0.3.0.tar.gz

Download URL llm_token_saver-0.3.0.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
587b7fa45722bc5fbf8372f42f1e7db6f9c301e0d03e7d0be79cf416db736baa
BLAKE2b-256 checksum
How to use checksums
1e601f864744cffc65f819f6ddd3f52db47b1542af2703301a956e1a5547dad5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.6

Release files / llm_token_saver-0.3.0-py3-none-any.whl

Download URL llm_token_saver-0.3.0-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e4d6a3dca5da1abf676a81ac74cfd1b2eeed8ff9d8c72da6c0a601b0a5f6cc57
BLAKE2b-256 checksum
How to use checksums
cf935963daa0113397cbda1e4a04541a3cedcd288ac7bb961205f3980cdd6b09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.6

Security for llm-token-saver 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files No provenance

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