@prefactor/langchain 0.1.0 → 0.1.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.
- package/README.md +169 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# @prefactor/langchain
|
|
2
|
+
|
|
3
|
+
LangChain.js integration for Prefactor observability. Provides automatic tracing of LLM calls, tool executions, and agent workflows with minimal setup.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @prefactor/langchain
|
|
9
|
+
# or
|
|
10
|
+
bun add @prefactor/langchain
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Most users should install `@prefactor/sdk` instead, which bundles both `@prefactor/core` and `@prefactor/langchain`.
|
|
14
|
+
|
|
15
|
+
## Peer Dependencies
|
|
16
|
+
|
|
17
|
+
This package requires LangChain.js v1.0.0 or later:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install langchain@^1.0.0
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { createAgent, tool } from 'langchain';
|
|
27
|
+
import { z } from 'zod';
|
|
28
|
+
import { init, shutdown } from '@prefactor/langchain';
|
|
29
|
+
|
|
30
|
+
// Initialize Prefactor
|
|
31
|
+
const middleware = init();
|
|
32
|
+
|
|
33
|
+
// Create agent with middleware
|
|
34
|
+
const agent = createAgent({
|
|
35
|
+
model: 'claude-sonnet-4-5-20250929',
|
|
36
|
+
tools: [],
|
|
37
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
38
|
+
middleware: [middleware],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// All operations are automatically traced
|
|
42
|
+
const result = await agent.invoke({
|
|
43
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Graceful shutdown
|
|
47
|
+
await shutdown();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Exports
|
|
51
|
+
|
|
52
|
+
### Main Entry Points
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import {
|
|
56
|
+
init, // Initialize SDK and return middleware
|
|
57
|
+
shutdown, // Flush spans and close connections
|
|
58
|
+
getTracer, // Get tracer for manual instrumentation
|
|
59
|
+
} from '@prefactor/langchain';
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Middleware
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { PrefactorMiddleware } from '@prefactor/langchain';
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Utilities
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { extractTokenUsage } from '@prefactor/langchain';
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Re-exports from @prefactor/core
|
|
75
|
+
|
|
76
|
+
For convenience, common types are re-exported:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import {
|
|
80
|
+
type Config,
|
|
81
|
+
type HttpTransportConfig,
|
|
82
|
+
type Span,
|
|
83
|
+
SpanStatus,
|
|
84
|
+
SpanType,
|
|
85
|
+
} from '@prefactor/langchain';
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Configuration
|
|
89
|
+
|
|
90
|
+
### Environment Variables
|
|
91
|
+
|
|
92
|
+
- `PREFACTOR_TRANSPORT`: `"stdio"` | `"http"` (default: `"stdio"`)
|
|
93
|
+
- `PREFACTOR_API_URL`: API endpoint for HTTP transport
|
|
94
|
+
- `PREFACTOR_API_TOKEN`: Authentication token
|
|
95
|
+
- `PREFACTOR_SAMPLE_RATE`: Sampling rate 0.0-1.0 (default: `1.0`)
|
|
96
|
+
|
|
97
|
+
### Programmatic Configuration
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { init } from '@prefactor/langchain';
|
|
101
|
+
|
|
102
|
+
// STDIO transport (default)
|
|
103
|
+
const middleware = init();
|
|
104
|
+
|
|
105
|
+
// HTTP transport
|
|
106
|
+
const middleware = init({
|
|
107
|
+
transportType: 'http',
|
|
108
|
+
httpConfig: {
|
|
109
|
+
apiUrl: 'https://api.prefactor.ai',
|
|
110
|
+
apiToken: process.env.PREFACTOR_API_TOKEN!,
|
|
111
|
+
agentId: 'my-agent',
|
|
112
|
+
agentVersion: '1.0.0',
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## What Gets Traced
|
|
118
|
+
|
|
119
|
+
The middleware automatically captures:
|
|
120
|
+
|
|
121
|
+
- **LLM Calls**: Model name, inputs, outputs, token usage
|
|
122
|
+
- **Tool Executions**: Tool name, inputs, outputs, duration
|
|
123
|
+
- **Agent Operations**: Full workflow with parent-child relationships
|
|
124
|
+
- **Errors**: Stack traces and error messages
|
|
125
|
+
|
|
126
|
+
## Manual Instrumentation
|
|
127
|
+
|
|
128
|
+
For operations not automatically traced:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { getTracer, SpanType } from '@prefactor/langchain';
|
|
132
|
+
|
|
133
|
+
const tracer = getTracer();
|
|
134
|
+
|
|
135
|
+
const span = tracer.startSpan({
|
|
136
|
+
name: 'custom-operation',
|
|
137
|
+
spanType: SpanType.TOOL,
|
|
138
|
+
inputs: { data: 'example' },
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
const result = await doWork();
|
|
143
|
+
tracer.endSpan(span, { outputs: { result } });
|
|
144
|
+
} catch (error) {
|
|
145
|
+
tracer.endSpan(span, { error });
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Graceful Shutdown
|
|
150
|
+
|
|
151
|
+
Always call `shutdown()` before your application exits to ensure all pending spans are flushed:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { shutdown } from '@prefactor/langchain';
|
|
155
|
+
|
|
156
|
+
process.on('SIGTERM', async () => {
|
|
157
|
+
await shutdown();
|
|
158
|
+
process.exit(0);
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Requirements
|
|
163
|
+
|
|
164
|
+
- Node.js >= 24.0.0
|
|
165
|
+
- LangChain.js >= 1.0.0
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
MIT
|