Skip to main content
# 🛡️ Token-Saver
[![PyPI version](https://img.shields.io/badge/pypi-v0.1.0-blue.svg)](https://pypi.org/project/token-saver/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)

**Token-Saver** 是一个极致轻量、无侵入的 LLM API 中间件。只需一行代码,就能自动优化你的 Prompt 结构,大幅提升上下文缓存(Context Cache)命中率,并自动修剪冗长的对话历史,帮你直接砍掉 50%+ 的 API 账单!
## ✨ 核心特性
- **⚡ 零侵入接入**:完全兼容 OpenAI 官方 SDK 格式,无需重写业务逻辑。
- **🎯 前缀稳定器 (Prefix Stabilizer)**:自动提取 System Prompt 中的动态变量(如时间戳、随机 ID)到末尾,确保请求前缀字节级一致,强制触发缓存命中。
- **✂️ 上下文瘦身 (Context Pruner)**:自动检测并截断超长对话历史,防止输入 Token 无限膨胀。
- **🤐 输出防废话 (Output Constraint)**:自动注入隐藏提示词,让大模型直接给结果,减少输出 Token 浪费。

## 📦 安装

```bash
pip install token-saver

🚀 极简用法

只需要用 TokenSaver 包装你现有的官方客户端,其他代码完全不用改:

from openai import OpenAI
from token_saver import TokenSaver

# 1. 初始化原生客户端
raw_client = OpenAI(api_key="your-api-key", base_url="https://api.deepseek.com/v1")

# 2. 用 TokenSaver 包裹它!
client = TokenSaver(client=raw_client)

# 3. 像平常一样调用 (Token-Saver 会在后台自动优化)
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "你是一个助手。当前时间:2024-05-20 12:00:00"},
        {"role": "user", "content": "给我讲个笑话"}
    ]
)

🛠️ 高级配置

你可以自定义需要保留的上下文轮数:

# 只保留最近的 5 条历史消息
client = TokenSaver(client=raw_client, max_history=5)

🤝 贡献与反馈

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


---

### 2. 添加 `pyproject.toml`(安装配置文件)

在你的本地仓库根目录,新建一个文件叫 `pyproject.toml`,把下面的代码贴进去。这是目前 Python 打包的标准格式:

```toml
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "token-saver"
version = "0.1.0"
description = "A seamless LLM API middleware to maximize context cache hits and reduce token costs."
readme = "README.md"
requires-python = ">=3.8"
license = { text = "MIT" }
dependencies = [
    "openai>=1.0.0",
]

Metadata

Release files for llm-token-saver 0.1.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.1.0
File Size Uploaded Provenance Actions
llm_token_saver-0.1.0.tar.gz 5.6 kB Without provenance Details for llm_token_saver-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 13.1 kB

Release files / llm_token_saver-0.1.0.tar.gz

Download URL llm_token_saver-0.1.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8e5ba20cdb3776553d3a37d20da2784c31b99e094632fca0810fe361ee66fc04
BLAKE2b-256 checksum
How to use checksums
cc7b82363d887e4c80508e087dc30aacca87acaeef95b1b9e64e82d0f961f083
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.1.0-py3-none-any.whl

Download URL llm_token_saver-0.1.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be442bf85a23101de46890dc1a0279b602e3aec348a1ce5ecece3acd6b3bcab3
BLAKE2b-256 checksum
How to use checksums
cb727944fa1156b97634235c61303e1f8e61ec97eda3ad7a1d584ac35227b296
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.1.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.1.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