@revenium/litellm 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +630 -0
  3. package/dist/client.d.ts +17 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +713 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/config.d.ts +42 -0
  8. package/dist/config.d.ts.map +1 -0
  9. package/dist/config.js +332 -0
  10. package/dist/config.js.map +1 -0
  11. package/dist/constants.d.ts +15 -0
  12. package/dist/constants.d.ts.map +1 -0
  13. package/dist/constants.js +101 -0
  14. package/dist/constants.js.map +1 -0
  15. package/dist/index.d.ts +42 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +189 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/prompt-extraction.d.ts +11 -0
  20. package/dist/prompt-extraction.d.ts.map +1 -0
  21. package/dist/prompt-extraction.js +201 -0
  22. package/dist/prompt-extraction.js.map +1 -0
  23. package/dist/tracking.d.ts +47 -0
  24. package/dist/tracking.d.ts.map +1 -0
  25. package/dist/tracking.js +299 -0
  26. package/dist/tracking.js.map +1 -0
  27. package/dist/types.d.ts +348 -0
  28. package/dist/types.d.ts.map +1 -0
  29. package/dist/types.js +3 -0
  30. package/dist/types.js.map +1 -0
  31. package/dist/utils/circuit-breaker.d.ts +114 -0
  32. package/dist/utils/circuit-breaker.d.ts.map +1 -0
  33. package/dist/utils/circuit-breaker.js +216 -0
  34. package/dist/utils/circuit-breaker.js.map +1 -0
  35. package/dist/utils/error-handling.d.ts +166 -0
  36. package/dist/utils/error-handling.d.ts.map +1 -0
  37. package/dist/utils/error-handling.js +306 -0
  38. package/dist/utils/error-handling.js.map +1 -0
  39. package/dist/utils/logger-types.d.ts +171 -0
  40. package/dist/utils/logger-types.d.ts.map +1 -0
  41. package/dist/utils/logger-types.js +210 -0
  42. package/dist/utils/logger-types.js.map +1 -0
  43. package/dist/utils/provider-detection.d.ts +43 -0
  44. package/dist/utils/provider-detection.d.ts.map +1 -0
  45. package/dist/utils/provider-detection.js +103 -0
  46. package/dist/utils/provider-detection.js.map +1 -0
  47. package/dist/utils/stop-reason.d.ts +58 -0
  48. package/dist/utils/stop-reason.d.ts.map +1 -0
  49. package/dist/utils/stop-reason.js +136 -0
  50. package/dist/utils/stop-reason.js.map +1 -0
  51. package/dist/utils/summary-printer.d.ts +23 -0
  52. package/dist/utils/summary-printer.d.ts.map +1 -0
  53. package/dist/utils/summary-printer.js +234 -0
  54. package/dist/utils/summary-printer.js.map +1 -0
  55. package/dist/utils/trace-fields.d.ts +10 -0
  56. package/dist/utils/trace-fields.d.ts.map +1 -0
  57. package/dist/utils/trace-fields.js +117 -0
  58. package/dist/utils/trace-fields.js.map +1 -0
  59. package/dist/utils/validation.d.ts +121 -0
  60. package/dist/utils/validation.d.ts.map +1 -0
  61. package/dist/utils/validation.js +451 -0
  62. package/dist/utils/validation.js.map +1 -0
  63. package/examples/README.md +321 -0
  64. package/examples/litellm-basic.ts +240 -0
  65. package/examples/litellm-streaming.ts +309 -0
  66. package/examples/prompt-capture.ts +128 -0
  67. package/package.json +85 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Revenium Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,630 @@
1
+ # Revenium Middleware for LiteLLM Node.js
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@revenium/litellm.svg)](https://www.npmjs.com/package/@revenium/litellm)
4
+ [![Node Versions](https://img.shields.io/node/v/@revenium/litellm.svg)](https://www.npmjs.com/package/@revenium/litellm)
5
+ [![Documentation](https://img.shields.io/badge/docs-revenium.io-blue)](https://docs.revenium.io)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ A comprehensive Node.js middleware that automatically tracks LiteLLM Proxy usage and sends metrics to a LiteLLM server as well as Revenium for billing and analytics. Features seamless HTTP interception with support for all LiteLLM providers - no code changes required! Works with both TypeScript and JavaScript projects.
9
+
10
+ ## Features
11
+
12
+ - **Seamless HTTP interception** - Automatically tracks all LiteLLM Proxy requests
13
+ - **Multi-provider support** - Works with OpenAI, Anthropic, Google, Azure, Cohere, and more
14
+ - **Chat completions & embeddings** - Full support for both operation types
15
+ - **Streaming support** - Real-time tracking with time-to-first-token metrics
16
+ - **Fire-and-forget tracking** - Will not block application execution with metering updates
17
+ - **Comprehensive analytics** - Track users, customers, and other custom metadata
18
+ - **Trace visualization fields** - Support for distributed tracing, workflow tracking, and advanced analytics
19
+ - **LiteLLM proxy integration** - Purpose-built for LiteLLM's proxy architecture
20
+
21
+ ## Package Migration
22
+
23
+ This package has been renamed from `revenium-middleware-litellm-node` to `@revenium/litellm` for better organization and simpler naming.
24
+
25
+ ### Migration Steps
26
+
27
+ If you're upgrading from the old package:
28
+
29
+ ```bash
30
+ # Uninstall the old package
31
+ npm uninstall revenium-middleware-litellm-node
32
+
33
+ # Install the new package
34
+ npm install @revenium/litellm
35
+ ```
36
+
37
+ **Update your imports:**
38
+
39
+ ```typescript
40
+ // Old import
41
+ import "revenium-middleware-litellm-node";
42
+
43
+ // New import
44
+ import "@revenium/litellm";
45
+ ```
46
+
47
+ All functionality remains exactly the same - only the package name has changed.
48
+
49
+ ## Installation
50
+
51
+ ```bash
52
+ npm install @revenium/litellm dotenv
53
+ npm install --save-dev typescript tsx @types/node # For TypeScript projects
54
+ ```
55
+
56
+ ## Environment Variables
57
+
58
+ Set your environment variables:
59
+
60
+ ```bash
61
+ export REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
62
+ export REVENIUM_METERING_BASE_URL=https://api.revenium.ai
63
+ export LITELLM_PROXY_URL=https://your-litellm-proxy.com
64
+ export LITELLM_API_KEY=your_litellm_api_key
65
+ export REVENIUM_DEBUG=true # Optional: for debug logging
66
+
67
+ # Optional: Terminal cost/metrics summary
68
+ export REVENIUM_PRINT_SUMMARY=true # or 'human' or 'json'
69
+ export REVENIUM_TEAM_ID=your_team_id # Required for cost retrieval
70
+ ```
71
+
72
+ Or create a `.env` file in the project root:
73
+
74
+ ```bash
75
+ # .env file
76
+ REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
77
+ REVENIUM_METERING_BASE_URL=https://api.revenium.ai
78
+ LITELLM_PROXY_URL=https://your-litellm-proxy.com
79
+ LITELLM_API_KEY=your_litellm_api_key
80
+ REVENIUM_DEBUG=true
81
+
82
+ # Optional: Terminal cost/metrics summary
83
+ REVENIUM_PRINT_SUMMARY=true # or 'human' or 'json'
84
+ REVENIUM_TEAM_ID=your_team_id # Required for cost retrieval
85
+ ```
86
+
87
+ ## Requirements
88
+
89
+ - Node.js >= 18.0.0
90
+ - LiteLLM Proxy server running and accessible
91
+ - Revenium API key (obtain from [app.revenium.ai](https://app.revenium.ai))
92
+
93
+ ## LiteLLM Proxy Server Installation
94
+
95
+ This middleware requires a running LiteLLM Proxy server. For installation instructions, see the [official LiteLLM documentation](https://docs.litellm.ai/docs/proxy/quick_start).
96
+
97
+ **Quick setup:**
98
+
99
+ ```bash
100
+ # Install LiteLLM
101
+ pip install litellm[proxy]
102
+
103
+ # Start the proxy server
104
+ litellm --config /path/to/config.yaml
105
+ ```
106
+
107
+ ## What Gets Tracked
108
+
109
+ The middleware automatically captures comprehensive usage data for both chat completions and embeddings:
110
+
111
+ ### Usage Metrics
112
+
113
+ - **Token Counts** - Input tokens, output tokens, total tokens
114
+ - **Model Information** - Model name, provider (OpenAI, Anthropic, Google, etc.)
115
+ - **Request Timing** - Request duration, time-to-first-token (for streaming)
116
+ - **Cost Calculation** - Estimated costs based on current pricing
117
+ - **Operation Type** - Chat completions or embeddings
118
+
119
+ ### Business Context (Optional)
120
+
121
+ - **User Tracking** - Subscriber ID, email, credentials
122
+ - **Organization Data** - Organization ID, product ID
123
+ - **Task Classification** - Task type, agent identifier, trace ID
124
+ - **Quality Metrics** - Response quality scores, task identifiers
125
+
126
+ ### Technical Details
127
+
128
+ - **API Endpoints** - Chat completions and embeddings
129
+ - **Request Types** - Streaming vs non-streaming (chat only)
130
+ - **Error Tracking** - Failed requests, error types, retry attempts
131
+ - **Environment Info** - Development vs production usage
132
+
133
+ ## Usage
134
+
135
+ The middleware automatically initializes when imported. Simply import it at the top of your application:
136
+
137
+ ```typescript
138
+ import "@revenium/litellm";
139
+ ```
140
+
141
+ That's it! All LiteLLM Proxy requests will now be automatically tracked.
142
+
143
+ ## API Overview
144
+
145
+ The middleware provides the following functions for advanced usage:
146
+
147
+ - **`initialize()`** - Manually initialize the middleware (auto-initializes on import)
148
+ - **`configure(config)`** - Set configuration programmatically instead of using environment variables
149
+ - **`isMiddlewareInitialized()`** - Check if the middleware is initialized and working
150
+ - **`getStatus()`** - Get detailed status information with fields: `initialized`, `patched`, `hasConfig`, `proxyUrl`
151
+ - **`resetInitializationState()`** - Reset initialization state (useful for testing)
152
+
153
+ **Example:**
154
+
155
+ ```typescript
156
+ import {
157
+ configure,
158
+ isMiddlewareInitialized,
159
+ getStatus,
160
+ } from "@revenium/litellm";
161
+
162
+ // Configure programmatically
163
+ configure({
164
+ reveniumMeteringApiKey: "hak_your_api_key",
165
+ reveniumMeteringBaseUrl: "https://api.revenium.ai",
166
+ litellmProxyUrl: "https://your-proxy.com",
167
+ litellmApiKey: "your_litellm_key",
168
+ });
169
+
170
+ // Check status
171
+ if (isMiddlewareInitialized()) {
172
+ console.log("Middleware is ready!");
173
+ console.log(getStatus());
174
+ }
175
+ ```
176
+
177
+ ## Quick Start Examples
178
+
179
+ Want to try it immediately? Access the examples directory to find sample scripts to demonstrate how to integrate Revenium's middleware into your existing code.
180
+
181
+ ### Steps 1-4: Run Built-in Examples and Get Started
182
+
183
+ ```bash
184
+ # 1. Install the package and dependencies
185
+ npm install @revenium/litellm dotenv
186
+ npm install --save-dev typescript tsx @types/node
187
+
188
+ # 2. Set your API keys (see Environment Variables above)
189
+
190
+ # 3. Run examples
191
+ REVENIUM_DEBUG=true npx tsx examples/litellm-basic.ts # Basic LiteLLM proxy usage with metadata
192
+ REVENIUM_DEBUG=true npx tsx examples/litellm-streaming.ts # Streaming, embeddings, and advanced features
193
+ ```
194
+
195
+ ### Step 4: Follow the Examples
196
+
197
+ Detailed examples are available to help you get started:
198
+
199
+ **For npm users**: After installing, examples are available in:
200
+
201
+ ```
202
+ node_modules/@revenium/litellm/examples/
203
+ ```
204
+
205
+ **For GitHub users**: Clone the repository and explore the `examples/` directory, or run the examples directly:
206
+
207
+ ```bash
208
+ npm run example:basic
209
+ npm run example:streaming
210
+ ```
211
+
212
+ See the [examples/README.md](https://github.com/revenium/revenium-middleware-litellm-node/blob/HEAD/examples/README.md) for detailed step-by-step guides, including TypeScript integration patterns and troubleshooting.
213
+
214
+ ## Advanced Usage
215
+
216
+ ### Adding Custom Metadata
217
+
218
+ Track users, organizations, agents, API keys, and other custom metadata using the optional metadata fields
219
+ shown in the examples folder.
220
+
221
+ ## LiteLLM Multi-Provider Features
222
+
223
+ **Universal LLM Support**: The middleware supports all LiteLLM providers with automatic usage tracking for both chat completions and embeddings.
224
+
225
+ ## API Support Matrix
226
+
227
+ The middleware has been tested and supports the following features:
228
+
229
+ | Feature | Chat Completions | Embeddings | Streaming |
230
+ | ---------------------------- | ---------------- | ---------- | --------- |
231
+ | Basic Requests | ✅ | ✅ | ✅ |
232
+ | Metadata Tracking | ✅ | ✅ | ✅ |
233
+ | Token Usage | ✅ | ✅ | ✅ |
234
+ | Cost Calculation | ✅ | ✅ | ✅ |
235
+ | Time-to-First-Token | ✅ | N/A | ✅ |
236
+ | Error Tracking | ✅ | ✅ | ✅ |
237
+ | Prompt Capture | ✅ | ❌ | ✅ |
238
+ | Multi-Provider (via LiteLLM) | ✅ | ✅ | ✅ |
239
+
240
+ **Supported Providers** (via LiteLLM Proxy):
241
+
242
+ - OpenAI
243
+ - Anthropic (Claude)
244
+ - Google (Gemini)
245
+ - Azure OpenAI
246
+ - Cohere
247
+ - And all other providers supported by LiteLLM
248
+
249
+ ## Configuration
250
+
251
+ ### Supported Environment Variables
252
+
253
+ | Variable | Required | Description |
254
+ | ---------------------------- | -------- | -------------------------------------------------- |
255
+ | `REVENIUM_METERING_API_KEY` | Yes | Your Revenium API key (starts with `hak_`) |
256
+ | `REVENIUM_METERING_BASE_URL` | No | Revenium API base URL (default: production) |
257
+ | `LITELLM_PROXY_URL` | Yes | Your LiteLLM Proxy URL (base URL or full endpoint) |
258
+ | `LITELLM_API_KEY` | No | LiteLLM API key (if proxy requires authentication) |
259
+ | `REVENIUM_DEBUG` | No | Set to `true` for debug logging |
260
+
261
+ ### Metadata Headers
262
+
263
+ Metadata headers help provide better analytics and tracking:
264
+
265
+ ```typescript
266
+ const headers = {
267
+ "x-revenium-subscriber-id": "user-123",
268
+ "x-revenium-subscriber-email": "user@example.com",
269
+ "x-revenium-subscriber-credential-name": "api-key",
270
+ "x-revenium-subscriber-credential": "credential-value",
271
+ "x-revenium-organization-id": "org-456",
272
+ "x-revenium-product-id": "chat-app",
273
+ "x-revenium-task-type": "document_analysis",
274
+ "x-revenium-trace-id": "trace-789",
275
+ "x-revenium-agent": "document-processor-v2",
276
+ "x-revenium-environment": "production",
277
+ "x-revenium-operation-subtype": "function_call",
278
+ "x-revenium-retry-number": "0",
279
+ "x-revenium-parent-transaction-id": "parent-txn-123",
280
+ "x-revenium-transaction-name": "Process Payment",
281
+ "x-revenium-region": "us-east-1",
282
+ "x-revenium-credential-alias": "My API Key",
283
+ "x-revenium-trace-type": "customer-support",
284
+ "x-revenium-trace-name": "User Session #12345",
285
+ };
286
+ ```
287
+
288
+ ### Trace Visualization Fields
289
+
290
+ The middleware supports 10 trace visualization fields for distributed tracing and workflow analytics:
291
+
292
+ | Field | Header | Environment Variable | Description |
293
+ | --------------------- | ---------------------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
294
+ | `environment` | `x-revenium-environment` | `REVENIUM_ENVIRONMENT`, `NODE_ENV`, `DEPLOYMENT_ENV` | Deployment environment (production, staging, etc.) |
295
+ | `operationSubtype` | `x-revenium-operation-subtype` | - | Additional operation detail (e.g., "function_call" when tools are used) |
296
+ | `retryNumber` | `x-revenium-retry-number` | `REVENIUM_RETRY_NUMBER` | Retry attempt number (0 for first attempt) |
297
+ | `parentTransactionId` | `x-revenium-parent-transaction-id` | `REVENIUM_PARENT_TRANSACTION_ID` | Parent transaction reference for distributed tracing |
298
+ | `transactionName` | `x-revenium-transaction-name` | `REVENIUM_TRANSACTION_NAME` | Human-friendly operation label |
299
+ | `region` | `x-revenium-region` | `AWS_REGION`, `AZURE_REGION`, `GCP_REGION`, `REVENIUM_REGION` | Cloud region or data center |
300
+ | `credentialAlias` | `x-revenium-credential-alias` | `REVENIUM_CREDENTIAL_ALIAS` | Human-readable credential name |
301
+ | `traceType` | `x-revenium-trace-type` | `REVENIUM_TRACE_TYPE` | Categorical identifier for grouping workflows (max 128 chars, alphanumeric + hyphens/underscores) |
302
+ | `traceName` | `x-revenium-trace-name` | `REVENIUM_TRACE_NAME` | Human-readable label for trace instances (max 256 chars) |
303
+ | `traceId` | `x-revenium-trace-id` | - | Unique identifier for a conversation or session |
304
+
305
+ These fields can be provided via HTTP headers or environment variables. The middleware automatically detects and validates them.
306
+
307
+ ### Metadata Fields Reference
308
+
309
+ The following table shows commonly used metadata fields that can be set via HTTP headers or environment variables:
310
+
311
+ | Field | Header | Use Case |
312
+ | -------------------------- | --------------------------------------- | --------------------------------------------------------------------- |
313
+ | `subscriberId` | `x-revenium-subscriber-id` | Track usage by end user for billing and analytics |
314
+ | `subscriberEmail` | `x-revenium-subscriber-email` | Associate requests with user email for support and reporting |
315
+ | `subscriberCredentialName` | `x-revenium-subscriber-credential-name` | Identify which API key or credential was used |
316
+ | `subscriberCredential` | `x-revenium-subscriber-credential` | Store credential value for audit trails |
317
+ | `organizationId` | `x-revenium-organization-id` | Multi-tenant tracking and cost allocation |
318
+ | `subscriptionId` | `x-revenium-subscription-id` | Track usage by subscription plan or tier |
319
+ | `productId` | `x-revenium-product-id` | Track usage across different products or features |
320
+ | `taskType` | `x-revenium-task-type` | Categorize requests by task (e.g., "summarization", "translation") |
321
+ | `traceId` | `x-revenium-trace-id` | Link multiple API calls in a conversation or session |
322
+ | `agent` | `x-revenium-agent` | Identify which AI agent or service made the request |
323
+ | `environment` | `x-revenium-environment` | Separate production, staging, and development usage |
324
+ | `operationSubtype` | `x-revenium-operation-subtype` | Add detail to operation type (e.g., "function_call") |
325
+ | `retryNumber` | `x-revenium-retry-number` | Track retry attempts for reliability analysis |
326
+ | `parentTransactionId` | `x-revenium-parent-transaction-id` | Build distributed traces across microservices |
327
+ | `transactionName` | `x-revenium-transaction-name` | Human-readable operation labels for dashboards |
328
+ | `region` | `x-revenium-region` | Track usage by geographic region or data center |
329
+ | `credentialAlias` | `x-revenium-credential-alias` | Friendly name for credentials in reports |
330
+ | `traceType` | `x-revenium-trace-type` | Group workflows by type (e.g., "customer-support", "data-processing") |
331
+ | `traceName` | `x-revenium-trace-name` | Human-readable trace instance labels |
332
+ | `capturePrompts` | `x-revenium-capture-prompts` | Enable/disable prompt capture for this request (true/false) |
333
+
334
+ ### Running Examples
335
+
336
+ ```bash
337
+ # 1. Install dependencies
338
+ npm install @revenium/litellm dotenv
339
+ npm install --save-dev typescript tsx @types/node # For TypeScript projects
340
+
341
+ # 2. Set environment variables (or use .env file)
342
+ export REVENIUM_METERING_API_KEY=hak_your_api_key
343
+ export REVENIUM_METERING_BASE_URL=https://api.revenium.ai
344
+ export LITELLM_PROXY_URL=https://your-proxy.com
345
+ export LITELLM_API_KEY=your_litellm_key
346
+
347
+ # 3. Run examples
348
+ REVENIUM_DEBUG=true npx tsx examples/litellm-basic.ts # Basic LiteLLM proxy usage
349
+ REVENIUM_DEBUG=true npx tsx examples/litellm-streaming.ts # Streaming, embeddings, and advanced features
350
+ ```
351
+
352
+ ## Terminal Cost/Metrics Summary
353
+
354
+ The middleware can print a cost and metrics summary to your terminal after each API request. This is useful for development and debugging.
355
+
356
+ ### Configuration
357
+
358
+ Enable terminal summary output using environment variables or programmatic configuration:
359
+
360
+ **Environment Variables:**
361
+
362
+ ```bash
363
+ # Enable human-readable summary (default format)
364
+ export REVENIUM_PRINT_SUMMARY=true
365
+
366
+ # Or specify format explicitly
367
+ export REVENIUM_PRINT_SUMMARY=human # Human-readable format
368
+ export REVENIUM_PRINT_SUMMARY=json # JSON format for log parsing
369
+
370
+ # Optional: Set team ID to fetch cost data
371
+ export REVENIUM_TEAM_ID=your_team_id
372
+ ```
373
+
374
+ **Programmatic Configuration:**
375
+
376
+ ```typescript
377
+ import { ConfigurationManager } from "@revenium/litellm";
378
+
379
+ ConfigurationManager.setConfig({
380
+ reveniumMeteringApiKey: "hak_your_api_key",
381
+ reveniumMeteringBaseUrl: "https://api.revenium.ai",
382
+ printSummary: true, // or 'human' or 'json'
383
+ teamId: "your_team_id", // Optional: for cost retrieval
384
+ });
385
+ ```
386
+
387
+ ### Output Formats
388
+
389
+ **Human-readable format** (`printSummary: true` or `printSummary: 'human'`):
390
+
391
+ ```
392
+ ============================================================
393
+ 📊 REVENIUM USAGE SUMMARY
394
+ ============================================================
395
+ 🤖 Model: gpt-4
396
+ 🏢 Provider: OpenAI
397
+ ⏱️ Duration: 1.23s
398
+
399
+ 💬 Token Usage:
400
+ 📥 Input Tokens: 150
401
+ 📤 Output Tokens: 75
402
+ 📊 Total Tokens: 225
403
+
404
+ 💰 Cost: $0.004500
405
+ 🔖 Trace ID: trace-abc-123
406
+ ============================================================
407
+ ```
408
+
409
+ **JSON format** (`printSummary: 'json'`):
410
+
411
+ ```json
412
+ {
413
+ "model": "gpt-4",
414
+ "provider": "OpenAI",
415
+ "durationSeconds": 1.23,
416
+ "inputTokenCount": 150,
417
+ "outputTokenCount": 75,
418
+ "totalTokenCount": 225,
419
+ "cost": 0.0045,
420
+ "traceId": "trace-abc-123"
421
+ }
422
+ ```
423
+
424
+ ### Cost Retrieval
425
+
426
+ - **Without `teamId`**: Shows token counts and duration, displays hint to set `REVENIUM_TEAM_ID`
427
+ - **With `teamId`**: Fetches actual cost from Revenium API with automatic retry logic
428
+ - **Cost pending**: Shows "(pending aggregation)" if cost data isn't available yet
429
+ - **Fire-and-forget**: Never blocks your application, even if cost fetch fails
430
+
431
+ ## Prompt Capture
432
+
433
+ The middleware can capture prompts and responses for analysis and debugging. This feature is **disabled by default** for privacy and performance.
434
+
435
+ ### Configuration
436
+
437
+ Enable prompt capture using environment variables, programmatic configuration, or per-request metadata:
438
+
439
+ **Environment Variable:**
440
+
441
+ ```bash
442
+ export REVENIUM_CAPTURE_PROMPTS=true
443
+ export REVENIUM_MAX_PROMPT_SIZE=50000 # Optional: default is 50000 characters
444
+ ```
445
+
446
+ **Programmatic Configuration:**
447
+
448
+ ```typescript
449
+ import { configure } from "@revenium/litellm";
450
+
451
+ configure({
452
+ reveniumMeteringApiKey: "hak_your_api_key",
453
+ reveniumMeteringBaseUrl: "https://api.revenium.ai",
454
+ litellmProxyUrl: "https://your-proxy.com",
455
+ capturePrompts: true,
456
+ maxPromptSize: 50000,
457
+ });
458
+ ```
459
+
460
+ **Per-Request via Headers:**
461
+
462
+ ```typescript
463
+ const LITELLM_PROXY_URL = process.env.LITELLM_PROXY_URL;
464
+
465
+ const headers = {
466
+ "x-revenium-capture-prompts": "true",
467
+ };
468
+
469
+ const response = await fetch(`${LITELLM_PROXY_URL}/chat/completions`, {
470
+ method: "POST",
471
+ headers: {
472
+ ...headers,
473
+ "Content-Type": "application/json",
474
+ },
475
+ body: JSON.stringify({
476
+ model: "gpt-4",
477
+ messages: [{ role: "user", content: "Hello!" }],
478
+ }),
479
+ });
480
+ ```
481
+
482
+ ### Security
483
+
484
+ The middleware automatically sanitizes credentials from captured prompts:
485
+
486
+ - API keys and tokens are redacted (e.g., `sk-***REDACTED***`)
487
+ - Bearer tokens are replaced with `Bearer ***REDACTED***`
488
+ - Passwords and secrets are sanitized
489
+ - Credential patterns in prompt content are automatically detected and redacted
490
+
491
+ ### Use Cases
492
+
493
+ - **Debugging**: Analyze failed requests by reviewing exact prompts and responses
494
+ - **Quality Assurance**: Review model outputs for accuracy and appropriateness
495
+ - **Compliance**: Maintain audit trails of AI interactions
496
+ - **Cost Analysis**: Understand which prompts generate the most tokens
497
+
498
+ ## How It Works
499
+
500
+ 1. **HTTP Interception**: Patches the global `fetch` function to intercept LiteLLM Proxy requests
501
+ 2. **Request Detection**: Identifies LiteLLM requests by URL pattern matching for both chat and embeddings
502
+ 3. **Metadata Extraction**: Extracts usage metadata from request headers
503
+ 4. **Response Processing**: Handles both streaming and non-streaming responses for chat and embeddings
504
+ 5. **Usage Tracking**: Sends detailed metrics to Revenium API asynchronously
505
+ 6. **Error Handling**: Implements retry logic and fails silently by default
506
+
507
+ The middleware never blocks your application - if Revenium tracking fails, your LiteLLM requests continue normally.
508
+
509
+ ## Troubleshooting
510
+
511
+ ### Common Issues
512
+
513
+ #### Middleware not tracking requests
514
+
515
+ - Ensure middleware is imported before making fetch requests
516
+ - Check that environment variables are loaded correctly
517
+ - Verify your `REVENIUM_METERING_API_KEY` starts with `hak_`
518
+ - Confirm `LITELLM_PROXY_URL` matches your proxy setup
519
+
520
+ #### LiteLLM proxy connection issues
521
+
522
+ - Verify LiteLLM proxy is running and accessible
523
+ - Check that `LITELLM_PROXY_URL` includes the correct base URL
524
+ - Ensure `LITELLM_API_KEY` is correct if proxy requires authentication
525
+
526
+ #### "403 Forbidden" errors
527
+
528
+ ```bash
529
+ # Verify your Revenium API key
530
+ export REVENIUM_METERING_API_KEY="hak_your_actual_key"
531
+
532
+ # Check your base URL doesn't have double paths
533
+ export REVENIUM_METERING_BASE_URL="https://api.revenium.ai"
534
+ ```
535
+
536
+ #### Streaming not being tracked
537
+
538
+ - Streaming usage is tracked when the stream completes
539
+ - Check debug logs for stream completion messages
540
+ - Ensure you're consuming the entire stream
541
+
542
+ #### Embeddings not being tracked
543
+
544
+ - Verify the endpoint URL includes `/embeddings` or `/v1/embeddings`
545
+ - Check that the request body includes the `model` field
546
+ - Ensure the response includes usage information
547
+
548
+ ### Debug Mode
549
+
550
+ Enable detailed logging to troubleshoot issues:
551
+
552
+ ```bash
553
+ export REVENIUM_DEBUG=true
554
+ node your-script.js
555
+ ```
556
+
557
+ This will show:
558
+
559
+ - `[Revenium] LiteLLM request intercepted`
560
+ - `[Revenium] Usage metadata extracted`
561
+ - `[Revenium] Revenium tracking successful`
562
+
563
+ ## Documentation
564
+
565
+ For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
566
+
567
+ ## Contributing
568
+
569
+ See [CONTRIBUTING.md](https://github.com/revenium/revenium-middleware-litellm-node/blob/HEAD/CONTRIBUTING.md)
570
+
571
+ ## Testing
572
+
573
+ The middleware includes comprehensive automated tests that fail the build when something is wrong.
574
+
575
+ ### Run All Tests
576
+
577
+ Run unit, integration, and performance tests:
578
+
579
+ ```bash
580
+ npm test
581
+ ```
582
+
583
+ ### Run Tests with Coverage
584
+
585
+ ```bash
586
+ npm run test:coverage
587
+ ```
588
+
589
+ ### Run Tests in Watch Mode
590
+
591
+ ```bash
592
+ npm run test:watch
593
+ ```
594
+
595
+ ### Test Requirements
596
+
597
+ All tests are designed to:
598
+
599
+ - ✅ Fail the build when something is wrong (`process.exit(1)`)
600
+ - ✅ Pass when everything works correctly (`process.exit(0)`)
601
+ - ✅ Provide clear error messages
602
+ - ✅ Test trace field validation, environment detection, and region detection
603
+
604
+ ## Code of Conduct
605
+
606
+ See [CODE_OF_CONDUCT.md](https://github.com/revenium/revenium-middleware-litellm-node/blob/HEAD/CODE_OF_CONDUCT.md)
607
+
608
+ ## Security
609
+
610
+ See [SECURITY.md](https://github.com/revenium/revenium-middleware-litellm-node/blob/HEAD/SECURITY.md)
611
+
612
+ ## License
613
+
614
+ This project is licensed under the MIT License - see the [LICENSE](https://github.com/revenium/revenium-middleware-litellm-node/blob/HEAD/LICENSE) file for details.
615
+
616
+ ## Support
617
+
618
+ For support and questions:
619
+
620
+ - Visit [docs.revenium.io](https://docs.revenium.io)
621
+ - Open an issue on [GitHub](https://github.com/revenium/revenium-middleware-litellm-node/issues)
622
+ - Contact support at [support@revenium.io](mailto:support@revenium.io)
623
+
624
+ ## Development
625
+
626
+ For development guidelines and setup, see [CONTRIBUTING.md](https://github.com/revenium/revenium-middleware-litellm-node/blob/HEAD/CONTRIBUTING.md)
627
+
628
+ ---
629
+
630
+ Built by Revenium
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Patch the global fetch function to intercept LiteLLM Proxy requests
3
+ */
4
+ export declare function patchHttpClient(): boolean;
5
+ /**
6
+ * Restore the original fetch function
7
+ */
8
+ export declare function unpatchHttpClient(): boolean;
9
+ /**
10
+ * Check if HTTP client is patched
11
+ */
12
+ export declare function isHttpClientPatched(): boolean;
13
+ /**
14
+ * Reset HTTP client manager (for testing)
15
+ */
16
+ export declare function resetHttpClientManager(): void;
17
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AA80BA;;GAEG;AACH,wBAAgB,eAAe,IAAI,OAAO,CAyCzC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,CAwB3C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAE7C;AAED;;GAEG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAE7C"}