tokenc
v0.2.0
Published
Node.js SDK for The Token Company API - Compress LLM inputs to reduce costs
Downloads
686
Maintainers
Readme
tokenc - Node.js SDK
Compress LLM prompts to reduce costs and latency. 100K tokens compressed in ~85ms. Zero dependencies.
Install
npm install tokencUsage
import { TokenClient } from "tokenc";
const client = new TokenClient({ apiKey: "your-api-key" });
const result = await client.compressInput({
input: "Your long prompt here...",
model: "bear-1.2",
aggressiveness: 0.5, // 0.1 = light, 0.5 = balanced, 0.9 = aggressive
});
console.log(result.output); // compressed text
console.log(result.tokensSaved); // tokens removed
console.log(result.compressionRatio); // e.g. 1.8xWith Custom Settings
import { TokenClient, CompressionSettings } from "tokenc";
const client = new TokenClient({ apiKey: "your-api-key" });
const result = await client.compressInput({
input: "Your text...",
model: "bear-1.2",
compressionSettings: new CompressionSettings({
aggressiveness: 0.7,
maxOutputTokens: 100,
}),
});Performance
Requests are gzip-compressed automatically. Reuse the TokenClient instance for connection pooling.
| Input Size | E2E Latency | Throughput | |---|---|---| | 10K tokens | 38ms | 198K tok/s | | 100K tokens | 85ms | 975K tok/s | | 1M tokens | 542ms | 1.5M tok/s |
Error Handling
import { TokenClient, AuthenticationError, RateLimitError, APIError } from "tokenc";
try {
const result = await client.compressInput({ input: "Your text...", model: "bear-1.2" });
} catch (error) {
if (error instanceof AuthenticationError) console.log("Invalid API key");
else if (error instanceof RateLimitError) console.log("Rate limit exceeded");
else if (error instanceof APIError) console.log(`API error: ${error.message}`);
}Requirements
Node.js >= 18
Links
- Get API keys: https://thetokencompany.com
- Issues: https://github.com/TheTokenCompany/tokenc-npm-sdk/issues
