Skip to main content
Documentation
Getting Started

TypeScript / Node.js Quickstart

Get your first Laghav-compressed LLM call running in TypeScript or Node.js in under 5 minutes.

Step 1 — Install the SDK

bash
npm install laghav
# or
yarn add laghav
# or
pnpm add laghav

Step 2 — Set your API key

bash
export LAGHAV_API_KEY=lgh_live_xxxxxxxxxxxx

Step 3 — Make your first call

first-call.ts
import LaghavClient from 'laghav'
const client = new LaghavClient({
apiKey: process.env.LAGHAV_API_KEY!,
})
const response = await client.complete({
messages: [{ role: 'user', content: 'Hey could you help me understand the revenue drop last quarter?' }],
model: 'auto', // Laghav picks cheapest capable model
})
// Standard chat response
console.log(response.choices[0].message.content)
// Laghav savings metadata
const { laghav_meta: meta } = response
console.log(`Tokens saved: ${meta.original_tokens - meta.compressed_tokens}`)
console.log(`Compression: ${(meta.compression_ratio * 100).toFixed(0)}%`)
console.log(`Quality score: ${meta.quality_score}/100`)
console.log(`Saved: $${meta.saved_usd.toFixed(4)}`)
console.log(`Model used: ${meta.model_requested}`)
ESM and CJS
The package ships both ESM (import) and CommonJS (require) builds. Works in Node 18+, Deno, Bun, and Edge runtimes.

Streaming

stream.ts
const stream = await client.complete({
messages: [{ role: 'user', content: prompt }],
model: 'auto',
stream: true,
})
for await (const chunk of stream) {
if (chunk.choices[0]?.delta?.content) {
process.stdout.write(chunk.choices[0].delta.content)
}
}
// Final chunk includes laghav_meta
const finalChunk = await stream.finalMessage()
console.log(finalChunk.laghav_meta.saved_usd)

With laghav_options

options.ts
const response = await client.complete({
messages,
model: 'auto',
laghav_options: {
compress: true,
route: true,
cache: true,
score: true,
max_aggressiveness: 0.7,
skip_rules: ['intent'],
mask_pii: false,
},
})

Error handling

errors.ts
import LaghavClient, { RateLimitError, BudgetExceededError } from 'laghav'
try {
const response = await client.complete({ messages })
} catch (e) {
if (e instanceof RateLimitError) {
await new Promise(r => setTimeout(r, e.retryAfter * 1000))
// retry
} else if (e instanceof BudgetExceededError) {
console.error(`Budget exceeded: ${e.budgetId}`)
}
}