@spendgraph/sdk 0.5.1 → 0.7.0
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/LICENSE +202 -0
- package/README.md +25 -80
- package/dist/ai.js +1 -25
- package/dist/client.d.ts +2 -1
- package/dist/client.js +1 -38
- package/dist/core/client/client.d.ts +2 -0
- package/dist/core/client/client.js +1 -132
- package/dist/core/client/errors.js +1 -17
- package/dist/core/client/index.js +1 -2
- package/dist/core/concurrency.d.ts +2 -0
- package/dist/core/concurrency.js +1 -0
- package/dist/core/money.d.ts +23 -0
- package/dist/core/money.js +1 -0
- package/dist/core/types.d.ts +4 -0
- package/dist/core/types.js +0 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +1 -5
- package/dist/langchain.js +1 -69
- package/dist/resources/alerts.js +1 -9
- package/dist/resources/cli.js +1 -21
- package/dist/resources/credentials.js +1 -15
- package/dist/resources/events.js +1 -18
- package/dist/resources/files.d.ts +35 -0
- package/dist/resources/files.js +1 -0
- package/dist/resources/index.d.ts +4 -2
- package/dist/resources/index.js +1 -13
- package/dist/resources/ingest.js +1 -27
- package/dist/resources/keys.js +1 -15
- package/dist/resources/playground.js +1 -9
- package/dist/resources/pricing.js +1 -36
- package/dist/resources/projects.js +1 -49
- package/dist/resources/prompts-admin.js +1 -21
- package/dist/resources/prompts.d.ts +13 -1
- package/dist/resources/prompts.js +1 -49
- package/dist/resources/stats.js +1 -21
- package/dist/resources/tools.d.ts +1 -1
- package/dist/resources/tools.js +1 -21
- package/dist/rollout/index.d.ts +1 -0
- package/dist/rollout/index.js +1 -1
- package/dist/rollout/rollout.d.ts +30 -0
- package/dist/rollout/rollout.js +1 -1
- package/dist/schema/index.d.ts +1 -1
- package/dist/schema/index.js +1 -2
- package/dist/schema/serialize/index.js +1 -1
- package/dist/schema/serialize/serialize.js +1 -31
- package/dist/schema/types/index.js +0 -1
- package/dist/schema/types/types.js +0 -1
- package/dist/schema/validate/index.js +1 -1
- package/dist/schema/validate/validate.d.ts +4 -0
- package/dist/schema/validate/validate.js +1 -92
- package/dist/track/index.js +1 -1
- package/dist/track/track.js +1 -331
- package/docs/client.mdx +85 -0
- package/docs/errors.mdx +82 -0
- package/docs/tracking.mdx +62 -0
- package/package.json +11 -8
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export const meta = {
|
|
2
|
+
title: "Tracking — spendgraph docs",
|
|
3
|
+
description:
|
|
4
|
+
"The three SDK functions: wrap a client, track an event by hand, and flush before a serverless runtime freezes.",
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
# Tracking
|
|
8
|
+
|
|
9
|
+
Three functions. Most apps only ever call the first.
|
|
10
|
+
|
|
11
|
+
## meter.wrap(client)
|
|
12
|
+
|
|
13
|
+
A proxy over an Anthropic or OpenAI client. After each call resolves, the provider's own usage block — `usage.input_tokens` or `usage.prompt_tokens` — is read and reported. No per-call code, and no wrapper object to thread through your app.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
const anthropic = meter.wrap(new Anthropic());
|
|
17
|
+
const openai = meter.wrap(new OpenAI());
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
It also reads cache counts off the reply, which is the main reason to prefer it over hand-rolling `track()` against a provider that supports caching.
|
|
21
|
+
|
|
22
|
+
## meter.track(event)
|
|
23
|
+
|
|
24
|
+
Manual reporting, for any provider or framework:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
meter.track({
|
|
28
|
+
model: "gemini-2.5-pro",
|
|
29
|
+
inputTokens: usage.promptTokenCount,
|
|
30
|
+
outputTokens: usage.candidatesTokenCount,
|
|
31
|
+
metadata: { feature: "search" }, // optional, ≤1KB
|
|
32
|
+
eventId: crypto.randomUUID(), // optional: retries dedupe server-side
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`metadata` is what the **Cost by tenant** and **Cost by feature** breakdowns group on. Two keys you will want on day one are `feature` and either `tenant` or `env` — without them, spend is one undifferentiated number and the dashboard can only tell you that it went up.
|
|
37
|
+
|
|
38
|
+
`eventId` makes a retry safe. Send the same id twice and the second is discarded server-side rather than double-counted.
|
|
39
|
+
|
|
40
|
+
## meter.flush()
|
|
41
|
+
|
|
42
|
+
Sends everything buffered right now.
|
|
43
|
+
|
|
44
|
+
<Callout tone="trap" title="Serverless runtimes freeze the moment you respond">
|
|
45
|
+
Vercel functions, Lambda, and Cloudflare Workers stop executing as soon as the response is sent — and the batched events go with them. `await meter.flush()` before returning from a route handler, or you will lose the tail of every request.
|
|
46
|
+
</Callout>
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
export async function POST(req: Request) {
|
|
50
|
+
const reply = await anthropic.messages.create(/* … */);
|
|
51
|
+
await meter.flush();
|
|
52
|
+
return Response.json(reply);
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
On a long-lived server you can ignore it — the batch timer handles things.
|
|
57
|
+
|
|
58
|
+
## Unknown models
|
|
59
|
+
|
|
60
|
+
A model id spendgraph has no price for is still stored, at $0, and flagged on the [Pricing](/pricing) page. Token counts are never thrown away for want of a price.
|
|
61
|
+
|
|
62
|
+
The ingest response also returns any such id in `unpricedModels`, and the SDK logs each one once. That is deliberate: a typo in a model name should surface while you are still wiring the integration, not as an inexplicably empty dashboard a fortnight later.
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spendgraph/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Track LLM input/output tokens and cost. Three functions, zero dependencies, fail-open.",
|
|
5
|
-
"license": "
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/fnLog0/spendgraph.git",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"types": "./dist/index.d.ts",
|
|
30
30
|
"import": "./dist/index.js"
|
|
31
31
|
},
|
|
32
|
+
"./docs/*": "./docs/*",
|
|
32
33
|
"./langchain": {
|
|
33
34
|
"types": "./dist/langchain.d.ts",
|
|
34
35
|
"import": "./dist/langchain.js"
|
|
@@ -36,16 +37,14 @@
|
|
|
36
37
|
"./ai": {
|
|
37
38
|
"types": "./dist/ai.d.ts",
|
|
38
39
|
"import": "./dist/ai.js"
|
|
39
|
-
}
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
40
42
|
},
|
|
41
43
|
"files": [
|
|
42
44
|
"dist",
|
|
45
|
+
"docs",
|
|
43
46
|
"README.md"
|
|
44
47
|
],
|
|
45
|
-
"scripts": {
|
|
46
|
-
"build": "tsc -p tsconfig.json --emitDeclarationOnly && tsc -p tsconfig.json --declaration false --removeComments",
|
|
47
|
-
"test": "vitest run"
|
|
48
|
-
},
|
|
49
48
|
"devDependencies": {
|
|
50
49
|
"typescript": "^5"
|
|
51
50
|
},
|
|
@@ -54,5 +53,9 @@
|
|
|
54
53
|
},
|
|
55
54
|
"publishConfig": {
|
|
56
55
|
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "rm -rf dist && tsc -p tsconfig.json --emitDeclarationOnly && tsc -p tsconfig.json --declaration false --removeComments && node ../../scripts/minify.mjs dist",
|
|
59
|
+
"test": "vitest run"
|
|
57
60
|
}
|
|
58
|
-
}
|
|
61
|
+
}
|