diff --git a/doc/agent-observability.md b/doc/agent-observability.md index 9b5b8b87e8c..5696ec266f9 100644 --- a/doc/agent-observability.md +++ b/doc/agent-observability.md @@ -63,6 +63,13 @@ The URL does not grant access. Run telemetry permission, company membership and task privacy checks still apply. The reference is absent when prompt capture is disabled. Tool argument values remain excluded from company traces. +Each OpenCode model usage span includes a finite, nonnegative provider-reported +USD charge as `braintrust.metrics.estimated_cost`. Braintrust maps this attribute +to `metrics.estimated_cost` for cost totals without a model-pricing lookup. +`paperclip.cost.usd` remains available as metadata. A reported zero stays zero; +an unknown charge stays absent. Repeated updates replace the same message's +usage span. Parent and tool spans do not repeat the charge. + The exporter sends once at turn completion with a three-second deadline, at most four requests in flight and no idle polling or persistent queue. Closing drains pending requests and marks an unfinished turn failed. Export failures emit a diff --git a/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts b/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts index 17fc3e2fe53..372768c3721 100644 --- a/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts +++ b/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts @@ -47,6 +47,46 @@ it("exports turn and tool content without raw payloads, reasoning, credentials o expect(spans[1].status.code).toBe(2); expect(spanAttributes(spans[2])).toMatchObject({ "gen_ai.usage.input_tokens": 12, "gen_ai.usage.cache_read.input_tokens": 4, "paperclip.cost.usd": 0.2 }); }); +it("exports reported model costs once per message as standard cost with content capture disabled", async () => { + const { observer, bodies } = setup({ ...environment, OTEL_LOG_USER_PROMPTS: "0", OTEL_LOG_ASSISTANT_RESPONSES: "0", OTEL_LOG_TOOL_DETAILS: "0", OTEL_LOG_TOOL_CONTENT: "0" }); + observer.observe(event("turn.submitted", { text: "private" })); + for (const [usageMessageId, costUsd] of [["glm", 0.01], ["glm", 0.03650488], ["deepseek", 0.00014054052], ["kimi", 0.0208086]] as const) { + observer.observe(event("item.completed", { kind: "usage", usageMessageId, usage: { input: 114, output: 84, cache: { read: 138368, write: 0 }, costUsd } })); + } + observer.observe(event("turn.completed")); + await observer.close(); + const resource = bodies[0].resourceSpans[0]; + expect(spanAttributes(resource.resource)).toEqual({ "service.name": "paperclip-opencode", "paperclip.company.id": "company", "paperclip.agent.id": "agent" }); + const spans = resource.scopeSpans[0].spans; + expect(spans).toHaveLength(4); + expect(spanAttributes(spans[0])).not.toHaveProperty("braintrust.metrics.estimated_cost"); + const costs = spans.slice(1).map((span: Parameters[0]) => { + const attrs = spanAttributes(span); + expect(attrs).toMatchObject({ "gen_ai.usage.input_tokens": 114, "gen_ai.usage.output_tokens": 84, "gen_ai.usage.cache_read.input_tokens": 138368, "gen_ai.usage.cache_creation.input_tokens": 0, "paperclip.company.id": "company", "paperclip.run.id": "run-one" }); + expect(attrs["braintrust.metrics.estimated_cost"]).toBe(attrs["paperclip.cost.usd"]); + expect(span.attributes.find(a => a.key === "braintrust.metrics.estimated_cost")?.value).toEqual({ doubleValue: attrs["paperclip.cost.usd"] }); + return attrs["braintrust.metrics.estimated_cost"]; + }); + expect(costs).toEqual([0.03650488, 0.00014054052, 0.0208086]); + expect(JSON.stringify(bodies)).not.toContain("private"); +}); +it.each([0, null, undefined, -1, NaN, Infinity, "0.12"])("preserves zero and omits unknown or invalid standard cost %s", async costUsd => { + const { observer, bodies } = setup(); + observer.observe(event("turn.submitted")); + observer.observe(event("item.completed", { kind: "usage", usageMessageId: "message", usage: { input: 1, costUsd: 0.1 } })); + observer.observe(event("item.completed", { kind: "usage", usageMessageId: "message", usage: { input: 2, costUsd } })); + observer.observe(event("turn.completed")); + await observer.close(); + const spans = bodies[0].resourceSpans[0].scopeSpans[0].spans; + expect(spans).toHaveLength(2); + const attrs = spanAttributes(spans[1]); + expect(attrs["gen_ai.usage.input_tokens"]).toBe(2); + if (costUsd === 0) expect(attrs).toMatchObject({ "braintrust.metrics.estimated_cost": 0, "paperclip.cost.usd": 0 }); + else { + expect(attrs).not.toHaveProperty("braintrust.metrics.estimated_cost"); + expect(attrs).not.toHaveProperty("paperclip.cost.usd"); + } +}); it("honors all capture switches while retaining usage and the correct warm-run identity", async () => { const { observer, bodies } = setup({ ...environment, OTEL_LOG_USER_PROMPTS: "0", OTEL_LOG_ASSISTANT_RESPONSES: "0", OTEL_LOG_TOOL_CONTENT: "0" }); for (const runId of ["first", "second"]) { diff --git a/packages/paperclip-runner/src/drivers/opencode/company-observability.ts b/packages/paperclip-runner/src/drivers/opencode/company-observability.ts index 24ecc757678..13662bab198 100644 --- a/packages/paperclip-runner/src/drivers/opencode/company-observability.ts +++ b/packages/paperclip-runner/src/drivers/opencode/company-observability.ts @@ -209,7 +209,9 @@ export function createOpenCodeCompanyObservability(input: { const values: Attributes = { "gen_ai.operation.name": "chat", "gen_ai.request.model": input.model }; for (const [name, value] of Object.entries({ "gen_ai.usage.input_tokens": usage.input, "gen_ai.usage.output_tokens": usage.output, "gen_ai.usage.cache_read.input_tokens": cache.read, "gen_ai.usage.cache_creation.input_tokens": cache.write, - "paperclip.cost.usd": usage.costUsd })) if (finite(value)) values[name] = value; + "paperclip.cost.usd": usage.costUsd, + // Braintrust maps this OTLP attribute to the explicit USD cost metric. + "braintrust.metrics.estimated_cost": usage.costUsd })) if (finite(value)) values[name] = value; const previous = turn.usage.get(key); turn.usage.set(key, { ...makeSpan("opencode.model.usage", previous?.start ?? time, values), id: previous?.id ?? id(), end: time }); } else turn.truncated = true; diff --git a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts index 84166b7c210..0688c946651 100644 --- a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts +++ b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts @@ -490,6 +490,15 @@ describe("OpenCodeServerDriver", () => { expect(bodies).toHaveLength(2); const roots = bodies.map(body => body.resourceSpans[0].scopeSpans[0].spans[0]); expect(roots.map(span => span.attributes.find((a: any) => a.key === "paperclip.run.id").value.stringValue)).toEqual(["first", "second"]); + for (const body of bodies) { + const spans = body.resourceSpans[0].scopeSpans[0].spans; + const usage = spans.filter((span: any) => span.name === "opencode.model.usage"); + if (body === bodies[0]) { + expect(usage).toHaveLength(1); + expect(usage[0].attributes).toContainEqual({ key: "braintrust.metrics.estimated_cost", value: { doubleValue: 0.001 } }); + } + expect(spans[0].attributes.some((attribute: any) => attribute.key === "braintrust.metrics.estimated_cost")).toBe(false); + } expect(JSON.stringify(bodies)).not.toContain("ingestion-canary"); } finally { await session?.close({ reason: "test" });