@nextsparkjs/plugin-langchain 0.1.0-beta.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/.env.example +41 -0
  2. package/api/observability/metrics/route.ts +110 -0
  3. package/api/observability/traces/[traceId]/route.ts +398 -0
  4. package/api/observability/traces/route.ts +205 -0
  5. package/api/sessions/route.ts +332 -0
  6. package/components/observability/CollapsibleJson.tsx +71 -0
  7. package/components/observability/CompactTimeline.tsx +75 -0
  8. package/components/observability/ConversationFlow.tsx +271 -0
  9. package/components/observability/DisabledMessage.tsx +21 -0
  10. package/components/observability/FiltersPanel.tsx +82 -0
  11. package/components/observability/ObservabilityDashboard.tsx +230 -0
  12. package/components/observability/SpansList.tsx +210 -0
  13. package/components/observability/TraceDetail.tsx +335 -0
  14. package/components/observability/TraceStatusBadge.tsx +39 -0
  15. package/components/observability/TracesTable.tsx +97 -0
  16. package/components/observability/index.ts +7 -0
  17. package/docs/01-getting-started/01-overview.md +196 -0
  18. package/docs/01-getting-started/02-installation.md +368 -0
  19. package/docs/01-getting-started/03-configuration.md +794 -0
  20. package/docs/02-core-concepts/01-architecture.md +566 -0
  21. package/docs/02-core-concepts/02-agents.md +597 -0
  22. package/docs/02-core-concepts/03-tools.md +689 -0
  23. package/docs/03-orchestration/01-graph-orchestrator.md +809 -0
  24. package/docs/03-orchestration/02-legacy-react.md +650 -0
  25. package/docs/04-advanced/01-observability.md +645 -0
  26. package/docs/04-advanced/02-token-tracking.md +469 -0
  27. package/docs/04-advanced/03-streaming.md +476 -0
  28. package/docs/04-advanced/04-guardrails.md +597 -0
  29. package/docs/05-reference/01-api-reference.md +1403 -0
  30. package/docs/05-reference/02-customization.md +646 -0
  31. package/docs/05-reference/03-examples.md +881 -0
  32. package/docs/index.md +85 -0
  33. package/hooks/observability/useMetrics.ts +31 -0
  34. package/hooks/observability/useTraceDetail.ts +48 -0
  35. package/hooks/observability/useTraces.ts +59 -0
  36. package/lib/agent-factory.ts +354 -0
  37. package/lib/agent-helpers.ts +201 -0
  38. package/lib/db-memory-store.ts +417 -0
  39. package/lib/graph/index.ts +58 -0
  40. package/lib/graph/nodes/combiner.ts +399 -0
  41. package/lib/graph/nodes/router.ts +440 -0
  42. package/lib/graph/orchestrator-graph.ts +386 -0
  43. package/lib/graph/prompts/combiner.md +131 -0
  44. package/lib/graph/prompts/router.md +193 -0
  45. package/lib/graph/types.ts +365 -0
  46. package/lib/guardrails.ts +230 -0
  47. package/lib/index.ts +44 -0
  48. package/lib/logger.ts +70 -0
  49. package/lib/memory-store.ts +168 -0
  50. package/lib/message-serializer.ts +110 -0
  51. package/lib/prompt-renderer.ts +94 -0
  52. package/lib/providers.ts +226 -0
  53. package/lib/streaming.ts +232 -0
  54. package/lib/token-tracker.ts +298 -0
  55. package/lib/tools-builder.ts +192 -0
  56. package/lib/tracer-callbacks.ts +342 -0
  57. package/lib/tracer.ts +350 -0
  58. package/migrations/001_langchain_memory.sql +83 -0
  59. package/migrations/002_token_usage.sql +127 -0
  60. package/migrations/003_observability.sql +257 -0
  61. package/package.json +28 -0
  62. package/plugin.config.ts +170 -0
  63. package/presets/lib/langchain.config.ts.preset +142 -0
  64. package/presets/templates/sector7/ai-observability/[traceId]/page.tsx +91 -0
  65. package/presets/templates/sector7/ai-observability/page.tsx +54 -0
  66. package/types/langchain.types.ts +274 -0
  67. package/types/observability.types.ts +270 -0
@@ -0,0 +1,469 @@
1
+ # Token & Cost Tracking
2
+
3
+ This guide covers the token usage tracking system for monitoring LLM costs and usage.
4
+
5
+ ## Overview
6
+
7
+ The token tracker provides:
8
+ - **Per-request tracking**: Input/output tokens for each LLM call
9
+ - **Cost calculation**: Automatic cost computation based on model pricing
10
+ - **Usage analytics**: Aggregated stats by user, team, model, and time period
11
+ - **Dashboard**: Visual usage monitoring
12
+
13
+ ---
14
+
15
+ ## How It Works
16
+
17
+ ```
18
+ LLM Request → Token Usage → Cost Calculation → Database → Analytics
19
+ ↓ ↓
20
+ inputTokens pricing table
21
+ outputTokens ↓
22
+ totalCost (USD)
23
+ ```
24
+
25
+ ---
26
+
27
+ ## Pricing Configuration
28
+
29
+ Default pricing per 1M tokens (USD):
30
+
31
+ ```typescript
32
+ const DEFAULT_PRICING = {
33
+ 'gpt-4o': { input: 5.00, output: 15.00 },
34
+ 'gpt-4o-mini': { input: 0.15, output: 0.60 },
35
+ 'gpt-4-turbo': { input: 10.00, output: 30.00 },
36
+ 'gpt-3.5-turbo': { input: 0.50, output: 1.50 },
37
+ 'claude-3-5-sonnet': { input: 3.00, output: 15.00 },
38
+ 'claude-3-opus': { input: 15.00, output: 75.00 },
39
+ 'claude-3-haiku': { input: 0.25, output: 1.25 },
40
+ // Local models are free
41
+ 'ollama/*': { input: 0, output: 0 },
42
+ }
43
+ ```
44
+
45
+ ### Cost Calculation
46
+
47
+ ```typescript
48
+ // Per-request cost formula
49
+ inputCost = (inputTokens / 1,000,000) * pricing.input
50
+ outputCost = (outputTokens / 1,000,000) * pricing.output
51
+ totalCost = inputCost + outputCost
52
+
53
+ // Example: gpt-4o-mini with 500 input + 200 output tokens
54
+ inputCost = (500 / 1,000,000) * 0.15 = $0.000075
55
+ outputCost = (200 / 1,000,000) * 0.60 = $0.000120
56
+ totalCost = $0.000195 (~$0.0002 per request)
57
+ ```
58
+
59
+ ---
60
+
61
+ ## Database Schema
62
+
63
+ **Table**: `langchain_token_usage`
64
+
65
+ | Column | Type | Description |
66
+ |--------|------|-------------|
67
+ | `id` | TEXT | Unique identifier |
68
+ | `userId` | TEXT | User who made request |
69
+ | `teamId` | TEXT | Team context |
70
+ | `sessionId` | TEXT | Session identifier |
71
+ | `provider` | TEXT | LLM provider (openai, anthropic) |
72
+ | `model` | TEXT | Model name |
73
+ | `inputTokens` | INTEGER | Input token count |
74
+ | `outputTokens` | INTEGER | Output token count |
75
+ | `totalTokens` | INTEGER | Total tokens |
76
+ | `inputCost` | DECIMAL(12,6) | Input cost USD |
77
+ | `outputCost` | DECIMAL(12,6) | Output cost USD |
78
+ | `totalCost` | DECIMAL(12,6) | Total cost USD |
79
+ | `agentName` | TEXT | Agent that made call |
80
+ | `metadata` | JSONB | Additional data |
81
+ | `createdAt` | TIMESTAMPTZ | Timestamp |
82
+
83
+ **RLS Policies**:
84
+ - Users can only see their own usage
85
+ - Team admins can see team-wide usage
86
+
87
+ ---
88
+
89
+ ## Token Tracker Service
90
+
91
+ ### Basic Usage
92
+
93
+ ```typescript
94
+ import { tokenTracker } from '@/contents/plugins/langchain/lib/token-tracker'
95
+
96
+ // Track usage after LLM call
97
+ await tokenTracker.trackUsage({
98
+ context: { userId, teamId },
99
+ sessionId: 'session-123',
100
+ provider: 'openai',
101
+ model: 'gpt-4o-mini',
102
+ usage: {
103
+ inputTokens: 150,
104
+ outputTokens: 80,
105
+ totalTokens: 230,
106
+ },
107
+ agentName: 'orchestrator',
108
+ })
109
+ ```
110
+
111
+ ### Calculate Cost
112
+
113
+ ```typescript
114
+ const costs = tokenTracker.calculateCost('gpt-4o-mini', {
115
+ inputTokens: 500,
116
+ outputTokens: 200,
117
+ totalTokens: 700,
118
+ })
119
+
120
+ console.log(costs)
121
+ // { inputCost: 0.000075, outputCost: 0.000120, totalCost: 0.000195 }
122
+ ```
123
+
124
+ ### Get Usage Stats
125
+
126
+ ```typescript
127
+ // Get user's usage for last 30 days
128
+ const stats = await tokenTracker.getUsage(
129
+ { userId, teamId },
130
+ '30d'
131
+ )
132
+
133
+ console.log(stats)
134
+ // {
135
+ // totalTokens: 150000,
136
+ // totalCost: 2.45,
137
+ // inputTokens: 100000,
138
+ // outputTokens: 50000,
139
+ // requestCount: 450,
140
+ // byModel: {
141
+ // 'gpt-4o-mini': { tokens: 120000, cost: 0.50 },
142
+ // 'claude-3-haiku': { tokens: 30000, cost: 1.95 }
143
+ // }
144
+ // }
145
+ ```
146
+
147
+ ### Get Daily Usage (Charts)
148
+
149
+ ```typescript
150
+ const daily = await tokenTracker.getDailyUsage(
151
+ { userId, teamId },
152
+ 30 // days
153
+ )
154
+
155
+ console.log(daily)
156
+ // [
157
+ // { date: '2024-12-23', tokens: 5000, cost: 0.08, requests: 25 },
158
+ // { date: '2024-12-22', tokens: 4500, cost: 0.07, requests: 22 },
159
+ // ...
160
+ // ]
161
+ ```
162
+
163
+ ### Get Team Usage (Admin)
164
+
165
+ ```typescript
166
+ const teamStats = await tokenTracker.getTeamUsage(teamId, '30d')
167
+
168
+ console.log(teamStats)
169
+ // {
170
+ // totalTokens: 500000,
171
+ // totalCost: 8.50,
172
+ // byUser: {
173
+ // 'user-1': { tokens: 200000, cost: 3.40 },
174
+ // 'user-2': { tokens: 300000, cost: 5.10 }
175
+ // }
176
+ // }
177
+ ```
178
+
179
+ ---
180
+
181
+ ## API Endpoints
182
+
183
+ ### Get User Usage
184
+
185
+ ```
186
+ GET /api/v1/theme/default/ai/usage
187
+ ```
188
+
189
+ **Query Parameters**:
190
+ - `period` - `today`, `7d`, `30d`, or `all` (default: `30d`)
191
+
192
+ **Response**:
193
+ ```json
194
+ {
195
+ "success": true,
196
+ "data": {
197
+ "totalTokens": 150000,
198
+ "totalCost": 2.45,
199
+ "inputTokens": 100000,
200
+ "outputTokens": 50000,
201
+ "requestCount": 450,
202
+ "byModel": {
203
+ "gpt-4o-mini": { "tokens": 120000, "cost": 0.50 }
204
+ }
205
+ }
206
+ }
207
+ ```
208
+
209
+ ### Get Team Usage (Admin)
210
+
211
+ ```
212
+ GET /api/v1/theme/default/ai/usage/team
213
+ ```
214
+
215
+ **Query Parameters**:
216
+ - `period` - `today`, `7d`, `30d`, or `all` (default: `30d`)
217
+
218
+ **Response**:
219
+ ```json
220
+ {
221
+ "success": true,
222
+ "data": {
223
+ "totalTokens": 500000,
224
+ "totalCost": 8.50,
225
+ "byUser": {
226
+ "user-1": { "tokens": 200000, "cost": 3.40 },
227
+ "user-2": { "tokens": 300000, "cost": 5.10 }
228
+ }
229
+ }
230
+ }
231
+ ```
232
+
233
+ ---
234
+
235
+ ## Dashboard
236
+
237
+ The usage dashboard is available at:
238
+
239
+ ```
240
+ /dashboard/settings/ai-usage
241
+ ```
242
+
243
+ ### Features
244
+
245
+ 1. **Summary Stats**
246
+ - Total tokens used
247
+ - Total cost
248
+ - Request count
249
+
250
+ 2. **Usage by Model**
251
+ - Breakdown per model
252
+ - Percentage allocation
253
+
254
+ 3. **Daily Chart**
255
+ - Token usage over time
256
+ - Cost trend
257
+ - Request volume
258
+
259
+ 4. **Period Selector**
260
+ - Today
261
+ - Last 7 days
262
+ - Last 30 days
263
+ - All time
264
+
265
+ ---
266
+
267
+ ## React Hook
268
+
269
+ ### useTokenUsage
270
+
271
+ ```typescript
272
+ import { useTokenUsage } from '@/contents/plugins/langchain/hooks/useTokenUsage'
273
+
274
+ function UsageStats() {
275
+ const { data, isLoading, error } = useTokenUsage({
276
+ period: '30d',
277
+ })
278
+
279
+ if (isLoading) return <Loading />
280
+
281
+ return (
282
+ <div>
283
+ <Stat label="Total Tokens" value={data.totalTokens.toLocaleString()} />
284
+ <Stat label="Total Cost" value={`$${data.totalCost.toFixed(2)}`} />
285
+ <Stat label="Requests" value={data.requestCount} />
286
+
287
+ <h3>By Model</h3>
288
+ {Object.entries(data.byModel).map(([model, stats]) => (
289
+ <div key={model}>
290
+ {model}: {stats.tokens.toLocaleString()} tokens (${stats.cost.toFixed(2)})
291
+ </div>
292
+ ))}
293
+ </div>
294
+ )
295
+ }
296
+ ```
297
+
298
+ ---
299
+
300
+ ## Integration
301
+
302
+ ### With Agent Factory
303
+
304
+ Token tracking is automatically integrated when using the agent factory:
305
+
306
+ ```typescript
307
+ // In agent-factory.ts
308
+ const response = await model.invoke(messages)
309
+
310
+ // After LLM call, tokens are automatically tracked
311
+ await tokenTracker.trackUsage({
312
+ context,
313
+ sessionId,
314
+ provider: modelConfig.provider,
315
+ model: modelConfig.model,
316
+ usage: {
317
+ inputTokens: response.usage?.prompt_tokens || 0,
318
+ outputTokens: response.usage?.completion_tokens || 0,
319
+ totalTokens: response.usage?.total_tokens || 0,
320
+ },
321
+ agentName,
322
+ })
323
+ ```
324
+
325
+ ### With Graph Orchestrator
326
+
327
+ The graph orchestrator tracks usage at trace level:
328
+
329
+ ```typescript
330
+ // End trace includes token stats
331
+ await tracer.endTrace(context, traceId, {
332
+ tokens: {
333
+ input: totalInputTokens,
334
+ output: totalOutputTokens,
335
+ total: totalInputTokens + totalOutputTokens,
336
+ },
337
+ cost: tokenTracker.calculateCost(model, usage).totalCost,
338
+ })
339
+ ```
340
+
341
+ ---
342
+
343
+ ## Custom Pricing
344
+
345
+ ### Override Default Pricing
346
+
347
+ ```typescript
348
+ const customPricing = {
349
+ ...DEFAULT_PRICING,
350
+ 'my-custom-model': { input: 1.00, output: 2.00 },
351
+ }
352
+
353
+ const costs = tokenTracker.calculateCost('my-custom-model', usage, customPricing)
354
+ ```
355
+
356
+ ### Wildcard Pricing
357
+
358
+ Use wildcards for provider-level pricing:
359
+
360
+ ```typescript
361
+ const pricing = {
362
+ 'openai/*': { input: 0.50, output: 1.50 }, // Default for all OpenAI
363
+ 'gpt-4o': { input: 5.00, output: 15.00 }, // Override specific model
364
+ }
365
+ ```
366
+
367
+ ---
368
+
369
+ ## Query Periods
370
+
371
+ | Period | Description | SQL |
372
+ |--------|-------------|-----|
373
+ | `today` | Current date | `>= CURRENT_DATE` |
374
+ | `7d` | Last 7 days | `>= now() - 7 days` |
375
+ | `30d` | Last 30 days | `>= now() - 30 days` |
376
+ | `all` | All time | No filter |
377
+
378
+ ---
379
+
380
+ ## Cost Optimization Tips
381
+
382
+ ### 1. Use Appropriate Models
383
+
384
+ ```typescript
385
+ // For simple routing: use cheap model
386
+ 'orchestrator': {
387
+ model: 'claude-3-haiku', // $0.25/$1.25 per 1M
388
+ }
389
+
390
+ // For complex synthesis: use capable model
391
+ 'combiner': {
392
+ model: 'gpt-4o-mini', // $0.15/$0.60 per 1M
393
+ }
394
+ ```
395
+
396
+ ### 2. Monitor by Model
397
+
398
+ Check which models consume most tokens:
399
+
400
+ ```typescript
401
+ const stats = await tokenTracker.getUsage(context, '30d')
402
+
403
+ // Sort by cost
404
+ Object.entries(stats.byModel)
405
+ .sort(([,a], [,b]) => b.cost - a.cost)
406
+ .forEach(([model, stats]) => {
407
+ console.log(`${model}: $${stats.cost.toFixed(2)}`)
408
+ })
409
+ ```
410
+
411
+ ### 3. Set Alerts
412
+
413
+ Monitor when usage exceeds thresholds:
414
+
415
+ ```typescript
416
+ const stats = await tokenTracker.getUsage(context, 'today')
417
+
418
+ if (stats.totalCost > DAILY_BUDGET) {
419
+ // Send alert
420
+ await notifyAdmin(`Daily AI budget exceeded: $${stats.totalCost}`)
421
+ }
422
+ ```
423
+
424
+ ### 4. Use Local Models
425
+
426
+ For development or non-critical tasks:
427
+
428
+ ```typescript
429
+ // Ollama models are free
430
+ 'dev-agent': {
431
+ provider: 'ollama',
432
+ model: 'llama3.2', // Free (local)
433
+ }
434
+ ```
435
+
436
+ ---
437
+
438
+ ## Troubleshooting
439
+
440
+ ### Missing Token Counts
441
+
442
+ Some providers don't return usage in responses. Check:
443
+
444
+ 1. Model supports usage metadata
445
+ 2. Response includes `usage` field
446
+ 3. Streaming mode may not include usage
447
+
448
+ ### Zero Cost
449
+
450
+ If costs are $0.00:
451
+ 1. Model might be using local provider (Ollama)
452
+ 2. Model not in pricing table
453
+ 3. Check for typos in model name
454
+
455
+ ### High Costs
456
+
457
+ If costs are unexpectedly high:
458
+ 1. Check model being used
459
+ 2. Look for long inputs/outputs
460
+ 3. Review request frequency
461
+ 4. Consider caching responses
462
+
463
+ ---
464
+
465
+ ## Related Documentation
466
+
467
+ - [Observability](./01-observability.md) - Includes token tracking in traces
468
+ - [Graph Orchestrator](../03-orchestration/01-graph-orchestrator.md) - Optimized for fewer LLM calls
469
+ - [Configuration](../01-getting-started/03-configuration.md) - Model selection