Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api): record token usage and team on TypeSafe spans (#5004)

Abimael Martell0010c5f2170e
diff --git a/apps/api/src/__tests__/lib/branding/jev.test.ts b/apps/api/src/__tests__/lib/branding/jev.test.ts
index 5c4b31408..19d37970b 100644
--- a/apps/api/src/__tests__/lib/branding/jev.test.ts
+++ b/apps/api/src/__tests__/lib/branding/jev.test.ts
@@ -566,6 +566,20 @@ describe("branding with Jev", () => {
withSpan.mockRestore();
});
+ it("tags the Jev span with the team so spend can be attributed", async () => {
+ respondWith(jevResponse());
+ const withSpan = vi.spyOn(tracer, "withSpan");
+
+ await enhanceBrandingWithLLM(baseInput(new CostTracking()));
+
+ const call = withSpan.mock.calls.find(c => c[0] === "typesafe.systemone");
+ expect(call?.[2]?.attributes).toMatchObject({
+ feature: "branding",
+ teamId: "team-jev",
+ });
+ withSpan.mockRestore();
+ });
+
it("keeps a listed team on the LLM unless the request asks for fast", async () => {
for (const mode of [undefined, "auto", "standard"] as const) {
await enhanceBrandingWithLLM({ ...baseInput(new CostTracking()), mode });
diff --git a/apps/api/src/lib/agent-hint-rules.ts b/apps/api/src/lib/agent-hint-rules.ts
index 2cc5ea263..80238d797 100644
--- a/apps/api/src/lib/agent-hint-rules.ts
+++ b/apps/api/src/lib/agent-hint-rules.ts
@@ -19,8 +19,8 @@ import type {
* cannot apply, emits nothing.
* - A condition on an absent signal holds only for `exists` with `false`.
*/
-export type AgentHintRuleScalar = string | number | boolean;
-export type AgentHintRuleOp =
+type AgentHintRuleScalar = string | number | boolean;
+type AgentHintRuleOp =
| "eq"
| "ne"
| "lt"
@@ -48,7 +48,7 @@ const MAX_NAME_LENGTH = 64;
const MAX_STRING_VALUE_LENGTH = 200;
const MAX_RULE_TEXT_LENGTH = 500;
const MAX_LIST_ARGUMENT = 100;
-export const MAX_RULE_HINTS = 3;
+const MAX_RULE_HINTS = 3;
const SIGNAL_NAME = /^[a-z][a-z0-9_]*$/;
const PLACEHOLDER = /\{([a-z][a-z0-9_]*)(?::(first|remaining)=(\d+))?\}/g;
diff --git a/apps/api/src/lib/agent-hints-provider.ts b/apps/api/src/lib/agent-hints-provider.ts
index e72aeabd2..c1a0d05de 100644
--- a/apps/api/src/lib/agent-hints-provider.ts
+++ b/apps/api/src/lib/agent-hints-provider.ts
@@ -43,7 +43,7 @@ const MAX_STORED_HINTS = 10;
const MAX_RESPONSE_BYTES = 64 * 1024;
const MAX_PROVIDER_HINTS = 2;
const MAX_TOTAL_HINTS = 3;
-export const AGENT_HINTS_PROVIDER_CONTRACT_VERSION = 2;
+const AGENT_HINTS_PROVIDER_CONTRACT_VERSION = 2;
// Fixed labels only: never put team IDs or provider-supplied values here.
export const agentHintsProviderRequestsTotal = new Counter({
diff --git a/apps/api/src/lib/branding/jev.ts b/apps/api/src/lib/branding/jev.ts
index df17a3a49..3dd27af1b 100644
--- a/apps/api/src/lib/branding/jev.ts
+++ b/apps/api/src/lib/branding/jev.ts
@@ -804,6 +804,7 @@ export async function enhanceBrandingWithJev(
setSpanAttributes(span, {
"typesafe.model": result.model,
"typesafe.usage.input_tokens": result.usage?.input_tokens,
+ "typesafe.usage.output_tokens": result.usage?.output_tokens,
});
return result as JevResponse;
},
@@ -817,6 +818,7 @@ export async function enhanceBrandingWithJev(
"branding.jev.questions": Object.keys(request.questions).length,
...(options.shadow ? { "branding.jev.shadow": true } : {}),
...(input.scrapeId ? { scrapeId: input.scrapeId } : {}),
+ ...(input.teamId ? { teamId: input.teamId } : {}),
},
},
);
diff --git a/apps/api/src/search/execute.test.ts b/apps/api/src/search/execute.test.ts
index 15b3a3ab6..6bd6aca29 100644
--- a/apps/api/src/search/execute.test.ts
+++ b/apps/api/src/search/execute.test.ts
@@ -160,6 +160,7 @@ describe("executeSearch safe search", () => {
expect.anything(),
20,
logger,
+ "team-1",
);
expect(result.response.web?.map(x => x.url)).toEqual(safe.map(x => x.url));
expect(result.totalResultsCount).toBe(10);
diff --git a/apps/api/src/search/execute.ts b/apps/api/src/search/execute.ts
index d3e9aca04..0a45a5905 100644
--- a/apps/api/src/search/execute.ts
+++ b/apps/api/src/search/execute.ts
@@ -207,7 +207,7 @@ export async function executeSearch(
!zeroDataRetention &&
!options.enterprise?.some(mode => mode === "zdr" || mode === "anon")
) {
- await removeExplicitResults(searchResponse, limit, logger);
+ await removeExplicitResults(searchResponse, limit, logger, teamId);
}
if (searchResponse.web && searchResponse.web.length > 0) {
diff --git a/apps/api/src/search/safe-search.test.ts b/apps/api/src/search/safe-search.test.ts
index 1c918d99d..483366936 100644
--- a/apps/api/src/search/safe-search.test.ts
+++ b/apps/api/src/search/safe-search.test.ts
@@ -11,6 +11,7 @@ vi.mock("@typesafe-ai/sdk", async importOriginal => ({
}));
vi.mock("../config", () => ({ config: mocks.config }));
+import * as tracer from "../lib/otel-tracer";
import { removeExplicitResults } from "./safe-search";
const logger = {
@@ -207,3 +208,26 @@ it("does nothing without a TypeSafe API key", async () => {
expect(mocks.systemOne).not.toHaveBeenCalled();
expect(response.web).toHaveLength(1);
});
+
+it("records Jev's token usage and the team on each call's span for spend tracking", async () => {
+ mocks.systemOne.mockResolvedValue({
+ model: "jev-1.13.0",
+ answers: { explicit: { type: "noul", noul: 0.1 } },
+ usage: { input_tokens: 180, output_tokens: 3 },
+ });
+ const withSpan = vi.spyOn(tracer, "withSpan");
+ const setAttributes = vi.spyOn(tracer, "setSpanAttributes");
+
+ await removeExplicitResults({ web: [web("safe1")] }, 1, logger, "team-1");
+
+ const call = withSpan.mock.calls.find(c => c[0] === "typesafe.systemone");
+ expect(call?.[2]?.attributes).toMatchObject({ teamId: "team-1" });
+ expect(setAttributes).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.objectContaining({
+ "typesafe.model": "jev-1.13.0",
+ "typesafe.usage.input_tokens": 180,
+ "typesafe.usage.output_tokens": 3,
+ }),
+ );
+});
diff --git a/apps/api/src/search/safe-search.ts b/apps/api/src/search/safe-search.ts
index aeb70b236..b76fe1c46 100644
--- a/apps/api/src/search/safe-search.ts
+++ b/apps/api/src/search/safe-search.ts
@@ -47,6 +47,7 @@ export async function removeExplicitResults(
response: SearchV2Response,
limit: number,
logger: Logger,
+ teamId?: string,
): Promise<void> {
const { web, news, images } = response;
if (!web?.length && !news?.length && !images?.length) return;
@@ -72,12 +73,15 @@ export async function removeExplicitResults(
return await withSpan(
"typesafe.systemone",
async callSpan => {
- const { model, answers } = await typesafe.systemOne(
+ const { model, answers, usage } = await typesafe.systemOne(
{ state: { result }, questions },
{ signal, timeout: 2000, retry: { maxRetries: 1 } },
);
setSpanAttributes(callSpan, {
"typesafe.model": model,
+ // Read by the LLM spend dashboard to price the call.
+ "typesafe.usage.input_tokens": usage?.input_tokens,
+ "typesafe.usage.output_tokens": usage?.output_tokens,
"search.safe_filter.explicit_probability":
answers.explicit.noul,
});
@@ -85,7 +89,10 @@ export async function removeExplicitResults(
},
{
kind: SpanKind.CLIENT,
- attributes: { "search.safe_filter.result_type": type },
+ attributes: {
+ "search.safe_filter.result_type": type,
+ ...(teamId ? { teamId } : {}),
+ },
},
);
} catch (error) {