@usefarol/sdk 0.1.0 → 0.1.2
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 +18 -5
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -2
- package/dist/index.mjs +8 -2
- package/package.json +1 -1
- package/src/index.ts +10 -1
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @usefarol/sdk
|
|
2
2
|
|
|
3
3
|
**AI agent observability for Node.js** — wrap your agent entrypoints with a single `trace()` helper to send runs, token usage, cost, and spans to [Farol](https://usefarol.dev).
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install
|
|
8
|
+
npm install @usefarol/sdk
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
Requires **Node.js 18+** (global `fetch`).
|
|
@@ -18,7 +18,7 @@ Create or copy your API key from the Farol app: **[usefarol.dev](https://usefaro
|
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
20
|
import Anthropic from "@anthropic-ai/sdk";
|
|
21
|
-
import { trace } from "
|
|
21
|
+
import { trace } from "@usefarol/sdk";
|
|
22
22
|
|
|
23
23
|
const client = new Anthropic();
|
|
24
24
|
|
|
@@ -60,7 +60,7 @@ await myAgent("Summarize this week’s metrics.");
|
|
|
60
60
|
|
|
61
61
|
```typescript
|
|
62
62
|
import OpenAI from "openai";
|
|
63
|
-
import { trace } from "
|
|
63
|
+
import { trace } from "@usefarol/sdk";
|
|
64
64
|
|
|
65
65
|
const openai = new OpenAI();
|
|
66
66
|
|
|
@@ -103,7 +103,7 @@ await myAgent("Hello!");
|
|
|
103
103
|
Use `run.startSpan(name, { type, metadata })` for tools, retrieval, or extra LLM steps. Call `span.end()` when the step finishes, or `span.end(error)` on failure. Unclosed spans are auto-ended when the run completes.
|
|
104
104
|
|
|
105
105
|
```typescript
|
|
106
|
-
import { trace } from "
|
|
106
|
+
import { trace } from "@usefarol/sdk";
|
|
107
107
|
|
|
108
108
|
const pipeline = trace(
|
|
109
109
|
async (run, query: string) => {
|
|
@@ -147,6 +147,18 @@ await pipeline("What is Farol?");
|
|
|
147
147
|
|
|
148
148
|
Set `captureIo: true` in `trace` options to include `span.input` / `span.output` in the payload (only when you assign them). **Do not enable for sensitive data** without reviewing compliance needs.
|
|
149
149
|
|
|
150
|
+
## Sampling
|
|
151
|
+
|
|
152
|
+
Set `sampleRate` to reduce the percentage of runs sent to Farol. Errors are always sent regardless of sample rate.
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
const myAgent = trace(fn, {
|
|
156
|
+
agentName: "my-agent",
|
|
157
|
+
farolKey: "frl_...",
|
|
158
|
+
sampleRate: 0.1, // send 10% of successful runs
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
150
162
|
## Options
|
|
151
163
|
|
|
152
164
|
| Option | Description |
|
|
@@ -157,6 +169,7 @@ Set `captureIo: true` in `trace` options to include `span.input` / `span.output`
|
|
|
157
169
|
| `model` | Model label on the run |
|
|
158
170
|
| `costPer1kInputTokens` / `costPer1kOutputTokens` | USD per 1k tokens for cost estimates |
|
|
159
171
|
| `captureIo` | When `true`, include span `input`/`output` if set |
|
|
172
|
+
| `sampleRate` | Fraction of successful runs to send (`0.0`–`1.0`). Errors always sent. Default `1.0`. |
|
|
160
173
|
|
|
161
174
|
## Build (from source)
|
|
162
175
|
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -91,7 +91,8 @@ function trace(fn, options) {
|
|
|
91
91
|
model = "unknown",
|
|
92
92
|
costPer1kInputTokens = 25e-5,
|
|
93
93
|
costPer1kOutputTokens = 125e-5,
|
|
94
|
-
captureIo = false
|
|
94
|
+
captureIo = false,
|
|
95
|
+
sampleRate = 1
|
|
95
96
|
} = options;
|
|
96
97
|
if (captureIo) {
|
|
97
98
|
console.warn(
|
|
@@ -117,7 +118,12 @@ function trace(fn, options) {
|
|
|
117
118
|
for (const span of run.spans) {
|
|
118
119
|
if (!span.endedAt) span.end();
|
|
119
120
|
}
|
|
120
|
-
|
|
121
|
+
const shouldSend = run.status === "error" || Math.random() <= sampleRate;
|
|
122
|
+
if (shouldSend) {
|
|
123
|
+
await sendToFarol(run, farolKey, farolEndpoint, captureIo);
|
|
124
|
+
} else {
|
|
125
|
+
console.log(`[Farol] Run sampled out (sampleRate=${sampleRate})`);
|
|
126
|
+
}
|
|
121
127
|
}
|
|
122
128
|
};
|
|
123
129
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -65,7 +65,8 @@ function trace(fn, options) {
|
|
|
65
65
|
model = "unknown",
|
|
66
66
|
costPer1kInputTokens = 25e-5,
|
|
67
67
|
costPer1kOutputTokens = 125e-5,
|
|
68
|
-
captureIo = false
|
|
68
|
+
captureIo = false,
|
|
69
|
+
sampleRate = 1
|
|
69
70
|
} = options;
|
|
70
71
|
if (captureIo) {
|
|
71
72
|
console.warn(
|
|
@@ -91,7 +92,12 @@ function trace(fn, options) {
|
|
|
91
92
|
for (const span of run.spans) {
|
|
92
93
|
if (!span.endedAt) span.end();
|
|
93
94
|
}
|
|
94
|
-
|
|
95
|
+
const shouldSend = run.status === "error" || Math.random() <= sampleRate;
|
|
96
|
+
if (shouldSend) {
|
|
97
|
+
await sendToFarol(run, farolKey, farolEndpoint, captureIo);
|
|
98
|
+
} else {
|
|
99
|
+
console.log(`[Farol] Run sampled out (sampleRate=${sampleRate})`);
|
|
100
|
+
}
|
|
95
101
|
}
|
|
96
102
|
};
|
|
97
103
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ export interface TraceOptions {
|
|
|
9
9
|
costPer1kInputTokens?: number;
|
|
10
10
|
costPer1kOutputTokens?: number;
|
|
11
11
|
captureIo?: boolean;
|
|
12
|
+
/** 0.0 to 1.0, default 1.0 */
|
|
13
|
+
sampleRate?: number;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
export interface SpanOptions {
|
|
@@ -109,6 +111,7 @@ export function trace<T extends unknown[], R>(
|
|
|
109
111
|
costPer1kInputTokens = 0.00025,
|
|
110
112
|
costPer1kOutputTokens = 0.00125,
|
|
111
113
|
captureIo = false,
|
|
114
|
+
sampleRate = 1.0,
|
|
112
115
|
} = options;
|
|
113
116
|
|
|
114
117
|
if (captureIo) {
|
|
@@ -142,7 +145,13 @@ export function trace<T extends unknown[], R>(
|
|
|
142
145
|
if (!span.endedAt) span.end();
|
|
143
146
|
}
|
|
144
147
|
|
|
145
|
-
|
|
148
|
+
const shouldSend =
|
|
149
|
+
run.status === "error" || Math.random() <= sampleRate;
|
|
150
|
+
if (shouldSend) {
|
|
151
|
+
await sendToFarol(run, farolKey, farolEndpoint, captureIo);
|
|
152
|
+
} else {
|
|
153
|
+
console.log(`[Farol] Run sampled out (sampleRate=${sampleRate})`);
|
|
154
|
+
}
|
|
146
155
|
}
|
|
147
156
|
};
|
|
148
157
|
}
|