Python SDK
The official Trunkate AI Python SDK provides synchronous bindings for Python 3.9+. It utilizes httpx internally for lightning-fast networking.
Installation
pip install trunkate-aiSetup & Configuration
The SDK automatically reads the TRUNKATE_API_KEY from your environment.
import os
from trunkate_ai import Trunkate
# Initializes using os.environ.get("TRUNKATE_API_KEY")
client = Trunkate()
# Alternatively, pass it explicitly:
# client = Trunkate(api_key="tk_live_...")Usage (Universal Budget)
Trunkate’s budget parameter is Universal. It accepts both absolute token counts and proactive percentage buffers.
# 1. Define your long prompt
prompt = "Your very long context..."
# OPTION A: Fixed Token Budget (Integer)
# Strict limit for precise cost control.
optimized = client.optimize(text=prompt, budget=500)
# OPTION B: Proactive Percentage Buffer (String)
# Maintain a dynamic "Smart Buffer" (e.g., 20% of input size).
proactive = client.optimize(text=prompt, budget="20%")Parameter Reference
| Parameter | Type | Required | Description |
|---|---|---|---|
text | str | Yes | The raw prompt to optimize. |
task | str | No | Optional guidance for preservation ranking. |
budget | int|str | No | Absolute tokens (e.g. 500) or percentage (e.g. "20%"). |
model | str | No | Target LLM (e.g. gpt-4o). |
Last updated on