|
| 1 | +# @getlago/agent-sdk |
| 2 | + |
| 3 | +Instrument LLM clients and emit usage events to [Lago](https://www.getlago.com) for billing. |
| 4 | +Authored in TypeScript, ships compiled JavaScript with `.d.ts` — works for both JS and TS consumers. |
| 5 | + |
| 6 | +```text |
| 7 | + ┌──────────────┐ |
| 8 | +your code ──────► │ wrapped client│ ──► provider (Bedrock / Mistral / …) |
| 9 | + └──────┬───────┘ |
| 10 | + │ (extract usage) |
| 11 | + ▼ |
| 12 | + ┌──────────────┐ |
| 13 | + │ Lago events │ ──► api.getlago.com |
| 14 | + └──────────────┘ |
| 15 | +``` |
| 16 | + |
| 17 | +## What it does |
| 18 | + |
| 19 | +- Wraps your existing LLM client in place — no API surface change for your application code. |
| 20 | +- Extracts usage from each response into a normalized shape (`CanonicalUsage`). |
| 21 | +- Buffers events in memory, flushes them in batches to Lago's `/events/batch` endpoint. |
| 22 | +- Survives provider/Lago outages with exponential backoff and a bounded buffer. |
| 23 | +- p99 wrap-overhead under 5 ms — your call is never blocked on Lago. |
| 24 | + |
| 25 | +## Install |
| 26 | + |
| 27 | +```bash |
| 28 | +npm install @getlago/agent-sdk |
| 29 | +# plus the provider SDK(s) you use: |
| 30 | +npm install @aws-sdk/client-bedrock-runtime |
| 31 | +npm install @mistralai/mistralai |
| 32 | +``` |
| 33 | + |
| 34 | +## Quickstart — Bedrock |
| 35 | + |
| 36 | +```typescript |
| 37 | +import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime"; |
| 38 | +import { LagoSDK } from "@getlago/agent-sdk"; |
| 39 | + |
| 40 | +const sdk = new LagoSDK({ |
| 41 | + apiKey: process.env.LAGO_API_KEY!, |
| 42 | + defaultSubscriptionId: "sub_acme", |
| 43 | +}); |
| 44 | +const client = sdk.wrap(new BedrockRuntimeClient({ region: "eu-west-1" })); |
| 45 | + |
| 46 | +await client.send(new ConverseCommand({ |
| 47 | + modelId: "eu.amazon.nova-lite-v1:0", |
| 48 | + messages: [{ role: "user", content: [{ text: "Hello" }] }], |
| 49 | +})); |
| 50 | +await sdk.flush(); |
| 51 | +``` |
| 52 | + |
| 53 | +The wrapped client behaves identically to the original — same arguments, same return shape, same exceptions. The SDK adds an in-memory queue that batches events to Lago in the background. |
| 54 | + |
| 55 | +## Quickstart — Mistral |
| 56 | + |
| 57 | +```typescript |
| 58 | +import { Mistral } from "@mistralai/mistralai"; |
| 59 | +import { LagoSDK } from "@getlago/agent-sdk"; |
| 60 | + |
| 61 | +const sdk = new LagoSDK({ apiKey: process.env.LAGO_API_KEY!, defaultSubscriptionId: "sub_acme" }); |
| 62 | +const client = sdk.wrap(new Mistral({ apiKey: process.env.MISTRAL_API_KEY! })); |
| 63 | + |
| 64 | +await client.chat.complete({ |
| 65 | + model: "mistral-small-latest", |
| 66 | + messages: [{ role: "user", content: "Hello" }], |
| 67 | +}); |
| 68 | +await sdk.flush(); |
| 69 | +``` |
| 70 | + |
| 71 | +## Multi-tenant — pick a subscription per call |
| 72 | + |
| 73 | +Three ways to set the `external_subscription_id`, in priority order: |
| 74 | + |
| 75 | +```typescript |
| 76 | +// 1. Per-call override — attach __lago to a Bedrock command, or pass `lago: {...}` on a Mistral call. |
| 77 | +const cmd = new ConverseCommand({...}); |
| 78 | +(cmd as any).__lago = { subscription: "sub_acme", dimensions: { feature: "summarize" } }; |
| 79 | +await client.send(cmd); |
| 80 | + |
| 81 | +// 2. Context-bound — uses AsyncLocalStorage; safe across `await` boundaries. |
| 82 | +sdk.withSubscription("sub_acme", async () => { |
| 83 | + await client.send(...); // bills sub_acme |
| 84 | +}); |
| 85 | +// or at the top of a request handler: |
| 86 | +sdk.setSubscription("sub_acme"); |
| 87 | + |
| 88 | +// 3. Default at init (fallback) |
| 89 | +new LagoSDK({ apiKey: "...", defaultSubscriptionId: "sub_default" }); |
| 90 | +``` |
| 91 | + |
| 92 | +Backed by Node's `AsyncLocalStorage` for safe propagation across promises. |
| 93 | + |
| 94 | +## Supported providers |
| 95 | + |
| 96 | +| Provider | Access | Status | |
| 97 | +|---|---|---| |
| 98 | +| AWS Bedrock | `ConverseCommand` (sync + stream) | ✓ | |
| 99 | +| AWS Bedrock | `InvokeModelCommand` (sync + stream), 7 model families | ✓ | |
| 100 | +| Mistral | `@mistralai/mistralai` (`chat.complete` + `chat.stream`) | ✓ | |
| 101 | +| OpenAI | native SDK | Phase 2 | |
| 102 | +| Anthropic | native SDK | Phase 2 | |
| 103 | +| Google Gemini | native SDK | Phase 2 | |
| 104 | +| Vercel AI SDK | `wrapLanguageModel` middleware | Phase 3 | |
| 105 | + |
| 106 | +## Token dimensions captured |
| 107 | + |
| 108 | +`CanonicalUsage` carries 10 numeric fields. Which ones populate depends on the provider: |
| 109 | + |
| 110 | +| Field | Lago metric code | Bedrock | Mistral native | |
| 111 | +|---|---|---|---| |
| 112 | +| input | `llm_input_tokens` | ✓ | ✓ | |
| 113 | +| output | `llm_output_tokens` | ✓ | ✓ | |
| 114 | +| cache_read | `llm_cached_input_tokens` | ✓ (Anthropic) | ✓ (when cache hits) | |
| 115 | +| cache_write | `llm_cache_creation_tokens` | ✓ (Anthropic) | ✗ | |
| 116 | +| cache_write_5m / 1h | `llm_cache_write_5m/1h_tokens` | ✓ (Anthropic InvokeModel) | ✗ | |
| 117 | +| reasoning | `llm_reasoning_tokens` | ✗ (folded into output) | ✗ (folded into output) | |
| 118 | +| tool_calls | `llm_tool_calls` | ✓ | ✓ | |
| 119 | +| image_input / audio_input | `llm_image/audio_input_tokens` | ✗ | ✗ | |
| 120 | + |
| 121 | +## Error policy |
| 122 | + |
| 123 | +The SDK never breaks your LLM call. If anything in instrumentation fails (adapter bug, Lago down, network error), the SDK swallows it, logs a warning, and your call returns normally. |
| 124 | + |
| 125 | +Wire your own observability via `onError`: |
| 126 | + |
| 127 | +```typescript |
| 128 | +new LagoSDK({ |
| 129 | + apiKey: "...", |
| 130 | + config: { |
| 131 | + onError: (err, where) => Sentry.captureException(err, { tags: { sdk_phase: where } }), |
| 132 | + }, |
| 133 | +}); |
| 134 | +``` |
| 135 | + |
| 136 | +## Setting up Lago |
| 137 | + |
| 138 | +The SDK ships with default metric codes (`llm_input_tokens`, `llm_output_tokens`, etc.). You need to register matching billable metrics in your Lago tenant before events count toward charges. See [Lago docs — Billable Metrics](https://docs.getlago.com/api-reference/billable-metrics/create). |
| 139 | + |
| 140 | +## Development |
| 141 | + |
| 142 | +```bash |
| 143 | +git clone https://github.com/getlago/lago-agent-sdk-js |
| 144 | +cd lago-agent-sdk-js |
| 145 | +npm install |
| 146 | +npm test |
| 147 | +npm run build |
| 148 | +``` |
| 149 | + |
| 150 | +Run live integration tests (requires real credentials): |
| 151 | + |
| 152 | +```bash |
| 153 | +AWS_BEARER_TOKEN_BEDROCK="..." \ |
| 154 | +MISTRAL_API_KEY="..." \ |
| 155 | +LAGO_API_URL="https://api.getlago.com/api/v1/" \ |
| 156 | +LAGO_API_KEY="..." \ |
| 157 | +LAGO_EXTERNAL_SUBSCRIPTION_ID="sub_..." \ |
| 158 | +npm test -- tests/integration |
| 159 | +``` |
| 160 | + |
| 161 | +## Security |
| 162 | + |
| 163 | +Found a vulnerability? See [SECURITY.md](SECURITY.md). |
0 commit comments