Skip to Content

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-ai

Setup & 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

ParameterTypeRequiredDescription
textstrYesThe raw prompt to optimize.
taskstrNoOptional guidance for preservation ranking.
budgetint|strNoAbsolute tokens (e.g. 500) or percentage (e.g. "20%").
modelstrNoTarget LLM (e.g. gpt-4o).
Last updated on