TokenSaver 提供简单的 REST API 和 Proxy 模式,让你可以以最少的改动集成 Token 压缩功能。
import requests
# 使用 API Key 压缩消息
headers = {"X-API-Key": "your-api-key"}
response = requests.post(
"https://api.tokesave.com/api/v1/compress",
headers=headers,
json={
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Summarize this data"}
]
}
)
result = response.json()
print(f"压缩率: {result['savings_percentage']:.1f}%")
print(f"节省费用: ${result['cost_saved_usd']:.4f}"){
"compressed_messages": [...], // 压缩后的消息数组
"tokens_before": 10000, // 原始 Token 数
"tokens_after": 2000, // 压缩后 Token 数
"savings_percentage": 80.0, // 压缩率
"transforms_applied": [ // 应用的压缩策略
"smart_crusher",
"cache_aligner"
],
"cost_saved_usd": 0.024 // 节省的费用(USD)
}