Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(topics): run manual processing through the trace-batch summary request (#18959)

Lysander Kiesel1c00c39270d4
diff --git a/packages/shared/src/server/topics/text.ts b/packages/shared/src/server/topics/text.ts
index b444cc32c8..e38d8e1b35 100644
--- a/packages/shared/src/server/topics/text.ts
+++ b/packages/shared/src/server/topics/text.ts
@@ -370,11 +370,15 @@ async function invokeWithCache(
recordResponse(result);
if (!message?.content || json.choices?.[0]?.finish_reason === "length")
- throw new Error(
- message?.refusal
- ? `The model refused: ${message.refusal}`
- : "The model returned no complete output.",
+ throw Object.assign(
+ new Error(
+ message?.refusal
+ ? `The model refused: ${message.refusal}`
+ : "The model returned no complete output.",
+ ),
+ { name: "AI_NoOutputGeneratedError" },
);
+ // A model can append text after a valid object; the caller treats that as unusable output.
return { ...result, output: JSON.parse(message.content) as unknown };
}
diff --git a/packages/shared/src/server/topics/trace-input.test.ts b/packages/shared/src/server/topics/trace-input.test.ts
index ec6b626db1..335dc81ecc 100644
--- a/packages/shared/src/server/topics/trace-input.test.ts
+++ b/packages/shared/src/server/topics/trace-input.test.ts
@@ -167,17 +167,15 @@ describe("Topics transcript input", () => {
});
});
- it("applies the Topics cap with stable equal-time ordering", async () => {
+ it("keeps the full transcript, like trace-batch, with stable equal-time ordering", async () => {
const rows = [0, 1, 2].map((index) => ({
span_id: `generation-${index}`,
input: JSON.stringify([{ role: "user", content: `Request ${index}` }]),
output: "response".repeat(4_000),
}));
const result = await loadRows(...rows);
- expect(result.transcript?.truncated).toBe(true);
- expect(JSON.stringify(result.transcript).length).toBeLessThanOrEqual(
- 10_000,
- );
+ expect(result.transcript?.truncated).toBeFalsy();
+ expect(JSON.stringify(result.transcript).length).toBeGreaterThan(96_000);
expect((await loadRows(...rows.reverse())).transcript).toEqual(
result.transcript,
);
diff --git a/packages/shared/src/server/topics/trace-input.ts b/packages/shared/src/server/topics/trace-input.ts
index 9d3475870d..f51bd6fc13 100644
--- a/packages/shared/src/server/topics/trace-input.ts
+++ b/packages/shared/src/server/topics/trace-input.ts
@@ -187,8 +187,7 @@ export async function loadTopicTranscript(params: {
sessionId,
environment,
traceName: traceName || rootName,
- transcript: assembleTranscript(orderObservations(observations), {
- maxCharacters: 10_000,
- }),
+ // Uncapped, like trace-batch; the summary input limit rejects oversized traces.
+ transcript: assembleTranscript(orderObservations(observations)),
};
}
diff --git a/worker/src/features/topics/README.md b/worker/src/features/topics/README.md
index ba033b8b25..8b7b6fa5a8 100644
--- a/worker/src/features/topics/README.md
+++ b/worker/src/features/topics/README.md
@@ -323,21 +323,21 @@ reads. Missing/expired payloads fail explicitly. A new execution with **Reuse st
summaries** can recover persisted results. Redis loss or a crash between a provider
response and saving it can repeat inference.
-Stored-summary reuse is opt-in and requires the same facet version, summary model
-and transcript producer version; it does not check content freshness. Compatible
+Stored-summary reuse is opt-in and requires the same facet version, summary model,
+transcript producer (`transcript_id`) and version; it does not check content freshness. Compatible
vectors are reused; changed dimensions regenerate only embeddings. Results receive
the new execution's staging ownership,
replace previous summaries, record reuse timestamps and count only new usage.
This is not a cross-execution deduplication cache.
-Each processing attempt shares identical transcript text across facets for a trace.
-Facet instructions affect only summarization. `loadTopicTranscript` uses the shared
-`orderObservations` and `assembleTranscript` producer, with a 10,000-character cap
-on the complete serialized JSON, including escaping, provenance and metadata.
-The model receives the structured transcript directly: threads with conversation
-history and current-turn messages, normalized parts and observation provenance.
-`truncated: true` marks omitted content. A null transcript produces
-`insufficient_input` without a summarization or embedding call.
+Manual processing and trace-batch share one summary request
+(`summarizeTraceFacets`): the facets of a trace that still need a summary go into
+one call on the same projected transcript. `loadTopicTranscript` uses the shared
+`orderObservations` and `assembleTranscript` producer without a cap, like
+trace-batch. The model receives the projected transcript: threads with
+conversation history and current-turn messages and normalized parts, without
+observation provenance. A null or empty transcript produces `insufficient_input`
+without a summarization or embedding call.
The shared producer defines observation eligibility, tool association and replay
deduplication. It includes generations and matched tool responses. Root-only
@@ -349,9 +349,9 @@ and limitations.
If transcript plus instructions/schema exceeds the execution's input allowance,
the worker fails before calling the provider; it does not
-silently change the evidence for that facet. Stored summaries record
-`transcript_id` (`poc`) and `transcript_version` (`shared-transcript-v3`), plus the
-models used. Historical summaries from other producer versions are regenerated.
+silently change the evidence. Stored summaries record
+`transcript_id` (`trace-batch`; older manual rows used `poc` with a capped transcript)
+and `transcript_version` (`shared-transcript-v3`), plus the models used. Historical summaries from other producer versions are regenerated.
Accepted Redis payloads retain their recorded version when the same execution
resumes. Transcripts are regenerated from current observations; original source
snapshots and content hashes are not retained.
@@ -359,7 +359,7 @@ snapshots and content hashes are not retained.
The summary prompt and schema write the summary before deciding applicability.
Check one trace after changing either prompt or schema before spending on a batch.
Replaying an accepted summary or embedding does not require its source trace.
-If a retry needs a missing facet, it uses the current transcript for that facet.
+If a retry needs missing facets, it summarizes them together from the current transcript.
UMAP and HDBSCAN propose density clusters. Original-space unit centroids and
cosine radii form the serving classifier. A radius uses the 95th percentile of
diff --git a/worker/src/features/topics/models.test.ts b/worker/src/features/topics/models.test.ts
index a2b8bf945e..f67a61f296 100644
--- a/worker/src/features/topics/models.test.ts
+++ b/worker/src/features/topics/models.test.ts
@@ -27,10 +27,12 @@ vi.mock("@langfuse/shared/topics/server", () => ({
import {
embedTopicSummaries,
nameTopicGroup,
- summarizeTopicTrace,
summarizeTopicTraceFacets,
} from "./models";
-import { topicProcessingConfigSchema } from "@langfuse/shared/topics";
+import {
+ topicProcessingConfigSchema,
+ type TopicProcessingConfig,
+} from "@langfuse/shared/topics";
import { LLMAdapter } from "@langfuse/shared";
import type { TopicsModel, TopicsModels } from "@langfuse/shared/topics/server";
@@ -99,6 +101,22 @@ afterEach(() => {
vi.unstubAllGlobals();
});
+// One built-in facet through the shared multi-facet request.
+const summarizeOne = (
+ models: TopicsModels,
+ text: string,
+ config: TopicProcessingConfig,
+) =>
+ summarizeTopicTraceFacets(
+ models,
+ [{ key: "intent", facet, builtIn: true }],
+ text,
+ config,
+ );
+const keyed = (output: Record<string, unknown>) => ({
+ intent: { notes: "Evidence.", ...output },
+});
+
describe("Topics naming boundary", () => {
it("keeps invalid cached Bedrock output per-trace and records paid usage", async () => {
const { generateTopicText } = await vi.importActual<
@@ -114,7 +132,9 @@ describe("Topics naming boundary", () => {
{
finish_reason: "stop",
message: {
- content: JSON.stringify({ summary: 42, status: "applicable" }),
+ content: JSON.stringify(
+ keyed({ summary: 42, status: "applicable" }),
+ ),
},
},
],
@@ -128,9 +148,8 @@ describe("Topics naming boundary", () => {
);
await expect(
- summarizeTopicTrace(
+ summarizeOne(
models,
- facet,
"Trace evidence.",
topicProcessingConfigSchema.parse({
summaryModel: models.summary.model,
@@ -147,6 +166,44 @@ describe("Topics naming boundary", () => {
});
});
+ it("classifies text after a valid cached Bedrock object as invalid output", async () => {
+ const { generateTopicText } = await vi.importActual<
+ typeof import("@langfuse/shared/topics/server")
+ >("@langfuse/shared/topics/server");
+ state.call.mockImplementation(generateTopicText);
+ state.env.LANGFUSE_TOPICS_TRACING_ENABLED_PROJECT_IDS = [];
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json({
+ choices: [
+ {
+ finish_reason: "stop",
+ message: {
+ content: `${JSON.stringify(keyed({ summary: "A request.", status: "applicable" }))} assistant {"intent_1":`,
+ },
+ },
+ ],
+ usage: { prompt_tokens: 12, completion_tokens: 4, total_tokens: 16 },
+ }),
+ ),
+ );
+ const models = topicsModels();
+ models.summary.connection.secretKey = encrypt(
+ JSON.stringify({ accessKeyId: "AKIATOPICS", secretAccessKey: "test" }),
+ );
+
+ await expect(
+ summarizeOne(
+ models,
+ "Trace evidence.",
+ topicProcessingConfigSchema.parse({
+ summaryModel: models.summary.model,
+ }),
+ ),
+ ).rejects.toMatchObject({ reason: "invalid_output" });
+ });
+
it.each([
["us.openai.gpt-5.6-luna", 0.00002, 0.000036],
["us.openai.gpt-6-luna", 0.000011, 0.0000165],
@@ -155,21 +212,19 @@ describe("Topics naming boundary", () => {
"summarizes a trace and prices %s usage",
async (model, inputCost, outputCost) => {
state.call.mockResolvedValue({
- output: { summary: "A billing request.", status: "applicable" },
+ output: keyed({ summary: "A billing request.", status: "applicable" }),
usage: { inputTokens: 100, outputTokens: 30 },
});
- const result = await summarizeTopicTrace(
+ const result = await summarizeOne(
topicsModels(model),
- facet,
"RAW_TRANSCRIPT_SENTINEL",
topicProcessingConfigSchema.parse({
summaryModel: model,
}),
);
- expect(result.output).toEqual({
- summary: "A billing request.",
- status: "applicable",
- });
+ expect(result.output).toEqual(
+ keyed({ summary: "A billing request.", status: "applicable" }),
+ );
expect(result.providedUsageDetails).toEqual({
summary_input: 100,
summary_output: 30,
@@ -192,24 +247,22 @@ describe("Topics naming boundary", () => {
aiFeatureOtelIngestion: false,
metadata: {
langfuse_project_id: "project",
- facet_id: "facet",
- facet_version: 1,
+ facets: [{ key: "intent", facet_id: "facet", facet_version: 1 }],
},
});
expect(request.system).toEqual([
{ text: expect.stringContaining(facet.prompt), cache: true },
]);
expect(request.input).toBe(
- "<transcript>\nRAW_TRANSCRIPT_SENTINEL\n</transcript>\n\nWrite the summary now, in the facet's format.",
+ "<transcript>\nRAW_TRANSCRIPT_SENTINEL\n</transcript>\n\nWrite the summaries now, one per facet key, each in its facet's format.",
);
},
);
it("rejects a request frozen on another summary model before calling the provider", async () => {
await expect(
- summarizeTopicTrace(
+ summarizeOne(
topicsModels("us.openai.gpt-6-luna"),
- facet,
"Trace evidence.",
topicProcessingConfigSchema.parse({
summaryModel: "us.openai.gpt-5.6-luna",
@@ -221,15 +274,14 @@ describe("Topics naming boundary", () => {
it("keeps fallback token counts out of provider-reported usage", async () => {
state.call.mockResolvedValue({
- output: { summary: "A billing request.", status: "applicable" },
+ output: keyed({ summary: "A billing request.", status: "applicable" }),
usage: { inputTokens: 100 },
});
const config = topicProcessingConfigSchema.parse({
summaryModel: "us.openai.gpt-5.6-luna",
});
- const result = await summarizeTopicTrace(
+ const result = await summarizeOne(
topicsModels(),
- facet,
"A request for an invoice.",
config,
);
@@ -250,9 +302,8 @@ describe("Topics naming boundary", () => {
});
it("rejects an oversized shared transcript before calling the provider", async () => {
await expect(
- summarizeTopicTrace(
+ summarizeOne(
topicsModels(),
- facet,
"Trace evidence. ".repeat(1000),
topicProcessingConfigSchema.parse({
maxInputTokens: 256,
@@ -267,7 +318,7 @@ describe("Topics naming boundary", () => {
it("prices cache reads and writes at their cache rates", async () => {
state.call.mockResolvedValue({
- output: { summary: "A billing request.", status: "applicable" },
+ output: keyed({ summary: "A billing request.", status: "applicable" }),
usage: {
inputTokens: 4000,
outputTokens: 300,
@@ -276,9 +327,8 @@ describe("Topics naming boundary", () => {
cacheWriteTokens: 100,
},
});
- const result = await summarizeTopicTrace(
+ const result = await summarizeOne(
topicsModels("us.openai.gpt-6-luna"),
- facet,
"RAW_TRANSCRIPT_SENTINEL",
topicProcessingConfigSchema.parse({
summaryModel: "us.openai.gpt-6-luna",
@@ -513,7 +563,10 @@ describe("Topics naming boundary", () => {
state.project.mockResolvedValue(organization ? { organization } : null);
state.call
.mockResolvedValueOnce({
- output: { summary: "A billing request.", status: "applicable" },
+ output: keyed({
+ summary: "A billing request.",
+ status: "applicable",
+ }),
usage: {},
})
.mockResolvedValueOnce({
@@ -524,9 +577,8 @@ describe("Topics naming boundary", () => {
},
usage: {},
});
- await summarizeTopicTrace(
+ await summarizeOne(
topicsModels(),
- facet,
"A billing request.",
topicProcessingConfigSchema.parse({
summaryModel: "us.openai.gpt-5.6-luna",
@@ -566,12 +618,11 @@ describe("Topics naming boundary", () => {
state.env.LANGFUSE_TOPICS_TRACING_ENABLED_PROJECT_IDS =
reason === "empty allowlist" ? [] : ["ai-features", "project-other"];
state.call.mockResolvedValue({
- output: { summary: "A billing request.", status: "applicable" },
+ output: keyed({ summary: "A billing request.", status: "applicable" }),
usage: {},
});
- await summarizeTopicTrace(
+ await summarizeOne(
topicsModels(),
- facet,
"A billing request.",
topicProcessingConfigSchema.parse({
summaryModel: "us.openai.gpt-5.6-luna",
diff --git a/worker/src/features/topics/models.ts b/worker/src/features/topics/models.ts
index 821a0ea38d..20ba7987e4 100644
--- a/worker/src/features/topics/models.ts
+++ b/worker/src/features/topics/models.ts
@@ -246,42 +246,6 @@ function requireFrozenSummaryModel(
);
}
-export async function summarizeTopicTrace(
- models: TopicsModels,
- facet: TopicFacetVersion,
- text: string,
- config: TopicProcessingConfig,
- metadata?: Record<string, unknown>,
-) {
- requireFrozenSummaryModel(models, config);
- const system = [
- {
- text: `${SUMMARY_SYSTEM_PROMPT}\n\n<facet>\n${facet.prompt}\n</facet>`,
- cache: true,
- },
- ];
- // Repeat the format request after the transcript; long inputs otherwise dilute it.
- const input = `<transcript>\n${text}\n</transcript>\n\nWrite the summary now, in the facet's format.`;
- // Include the structured-output schema and message framing in the input limit.
- const countedInputTokens =
- countTopicRequestTokens(system, input, summarySchema) + 256;
- if (countedInputTokens > config.maxInputTokens)
- throw new Error(
- `The shared trace transcript and instructions are ${countedInputTokens} tokens, above this run's ${config.maxInputTokens}-token input limit. No model call was made; the transcript is never shortened per facet.`,
- );
- return structuredCall(
- system,
- input,
- summarySchema,
- config.maxInputTokens,
- config.maxOutputTokens,
- "summary",
- models.summary,
- models.projectId,
- { ...metadata, facet_id: facet.facetId, facet_version: facet.version },
- );
-}
-
/**
* Summarizes all given facets of one trace in a single call. The system prompt
* is identical across traces; each facet is listed under its key and answered
diff --git a/worker/src/features/topics/processTopicsExecution.test.ts b/worker/src/features/topics/processTopicsExecution.test.ts
index 4eba4999a1..1ff8f90d2e 100644
--- a/worker/src/features/topics/processTopicsExecution.test.ts
+++ b/worker/src/features/topics/processTopicsExecution.test.ts
@@ -15,15 +15,15 @@ import {
type TopicEmbeddingBatch,
type TopicEmbeddingRef,
} from "@langfuse/shared/topics/server";
-import {
- assembleTranscript,
- type Transcript,
-} from "@langfuse/shared/src/server";
+import { assembleTranscript } from "@langfuse/shared/src/server";
import {
topicExecutionInputSchema,
topicSourceKey,
} from "@langfuse/shared/topics";
-import { topicProviderError } from "./provider-error";
+import {
+ TopicsProviderUnavailable,
+ topicProviderError,
+} from "./provider-error";
const state = vi.hoisted(() => ({
batches: new Map<string, TopicProcessBatchState>(),
@@ -46,6 +46,7 @@ const state = vi.hoisted(() => ({
saveExecution: vi.fn(),
loadTranscript: vi.fn(),
summarize: vi.fn(),
+ summaryRequests: vi.fn(),
embed: vi.fn(),
name: vi.fn(),
numeric: vi.fn(),
@@ -181,7 +182,15 @@ vi.mock("@langfuse/shared/topics/server", async (importOriginal) => {
facetId: string,
version: number,
) => state.facets.get(`${facetId}/${version}`) ?? null,
- listTopicFacets: async () => [],
+ listTopicFacets: async () =>
+ [...state.facets.values()].map((version) => ({
+ id: version.facetId,
+ projectId: version.projectId,
+ name: version.facetId,
+ description: "",
+ isBuiltIn: false,
+ versions: [version],
+ })),
listTopicSummaries: async (
projectId: string,
filter: {
@@ -315,8 +324,22 @@ vi.mock("@langfuse/shared/topics/server", async (importOriginal) => {
};
});
vi.mock("./models", () => ({
- summarizeTopicTrace: (_models: unknown, ...args: unknown[]) =>
- state.summarize(...args),
+ // One request per trace; `state.summarize` answers each facet, and one failure fails the request.
+ summarizeTopicTraceFacets: async (
+ _models: unknown,
+ facets: { key: string; facet: TopicFacetVersion }[],
+ text: string,
+ ) => {
+ state.summaryRequests(facets.length);
+ const output: Record<string, unknown> = {};
+ let usage = {};
+ for (const { key, facet } of facets) {
+ const result = await state.summarize(facet, text);
+ output[key] = { notes: "", ...result.output };
+ usage = { ...result, ...usage };
+ }
+ return { ...usage, output };
+ },
// `state.embed` sees each summary; one failure fails the whole batched call.
embedTopicSummaries: async (
_model: unknown,
@@ -526,9 +549,10 @@ beforeEach(() => {
traceInput(traceId),
);
state.summarize.mockImplementation(async (_facet, text: string) => {
- const transcript: Transcript = JSON.parse(text);
- const input = transcript.threads[0].currentTurn.messages[0].parts.find(
- (part) => part.type === "text",
+ // The projected transcript the trace-batch path sends.
+ const transcript = JSON.parse(text);
+ const input = transcript.threads[0].currentTurn[0].parts.find(
+ (part: { type: string }) => part.type === "text",
);
return {
output: {
@@ -719,6 +743,8 @@ describe("Topics execution", () => {
});
expect(state.loadTranscript).toHaveBeenCalledTimes(100);
expect(state.summarize).toHaveBeenCalledTimes(200);
+ // Like trace-batch, both facets of a trace share one summary request.
+ expect(state.summaryRequests).toHaveBeenCalledTimes(100);
expect(state.summaries.size).toBe(200);
state.executions.set(
"maps",
@@ -986,6 +1012,15 @@ describe("Topics execution", () => {
).toHaveLength(4);
});
+ it("does not reuse summaries of the capped legacy transcript", async () => {
+ await processSelection("legacy", 1);
+ for (const [key, row] of state.summaries)
+ state.summaries.set(key, { ...row, transcriptId: "poc" });
+ state.summarize.mockClear();
+ await processSelection("current", 1, undefined, true);
+ expect(state.summarize).toHaveBeenCalledOnce();
+ });
+
it("regenerates a stored summary when late observations move the source start", async () => {
await processSelection("source", 1);
state.loadTranscript.mockResolvedValue({
@@ -1015,6 +1050,50 @@ describe("Topics execution", () => {
expect(state.summarize).toHaveBeenCalledTimes(2);
});
+ it("fails only the trace whose model output is unusable", async () => {
+ state.summarize.mockRejectedValueOnce(
+ new TopicsProviderUnavailable("No object generated.", "invalid_output"),
+ );
+ await processSelection("invalid-output", 3);
+ expect(state.executions.get("invalid-output")?.status).toBe(
+ "completed_with_errors",
+ );
+ expect(
+ state.executions.get("invalid-output")?.facets[0].counts,
+ ).toMatchObject({ complete: 2, failed: 1 });
+ });
+
+ it.each(["summary call", "reuse lookup"])(
+ "keeps a reused facet when its sibling's %s fails",
+ async (failing) => {
+ await processSelection("source", 1);
+ const sibling = { ...facet, facetId: "sibling" };
+ const reprocess = execution("siblings", 1, "process", [facet, sibling]);
+ reprocess.input.reuseExistingSummaries = true;
+ state.executions.set("siblings", reprocess);
+ if (failing === "summary call")
+ state.summarize.mockRejectedValueOnce(
+ new Error("The transcript is above this run's input limit."),
+ );
+ else {
+ let lookups = 0;
+ // The first lookup reuses `facet`; the sibling's lookup fails.
+ state.resultReads.mockImplementation((kind: string) => {
+ if (kind === "historical" && ++lookups === 2)
+ throw new Error("ClickHouse is unavailable.");
+ });
+ }
+ await processTopicsExecution({
+ projectId: "project",
+ executionId: "siblings",
+ });
+ expect(state.executions.get("siblings")?.facets).toMatchObject([
+ { facetId: "facet", counts: { complete: 1, failed: 0 } },
+ { facetId: "sibling", counts: { failed: 1 } },
+ ]);
+ },
+ );
+
it("restarts a failed update with fresh membership and naming while preserving the published map", async () => {
await processSelection("source", 100);
await updateSelection("published");
diff --git a/worker/src/features/topics/processTopicsExecution.ts b/worker/src/features/topics/processTopicsExecution.ts
index 86aa1aa4bf..c86e184f2b 100644
--- a/worker/src/features/topics/processTopicsExecution.ts
+++ b/worker/src/features/topics/processTopicsExecution.ts
@@ -41,7 +41,12 @@ import {
} from "@langfuse/shared/topics";
import pLimit from "p-limit";
import { env } from "../../env";
-import { summarizeTopicTrace, nameTopicGroup } from "./models";
+import { nameTopicGroup } from "./models";
+import {
+ ASSEMBLED_TRANSCRIPT_ID,
+ summarizeTraceFacets,
+ type TraceFacet,
+} from "./summarizeAssembledTrace";
import {
isRejectedConnection,
pauseForRejectedConnection,
@@ -62,7 +67,7 @@ import {
} from "./numeric";
import { matchTopicContinuity } from "./continuity";
import { TopicMetrics } from "./metrics";
-import { topicSummaryOutputError } from "./summaryResult";
+import { splitTopicModelUsage } from "./summaryResult";
type ProcessExecution = TopicExecution & {
input: Extract<TopicExecution["input"], { operation: "process" }>;
@@ -79,16 +84,10 @@ const errorMessage = (error: unknown) =>
const isExecutionFailure = (error: unknown) =>
error instanceof TopicsProviderUnavailable ||
error instanceof TopicEmbeddingFailure;
-
-function invalidOutput(
- metrics: TopicMetrics,
- stage: "summary",
- message: string,
-) {
- const error = new Error(message);
- metrics.error(stage, error, "invalid_output");
- return error;
-}
+// Like trace-batch, a rejected input or unusable output fails only that trace.
+const isTraceModelFailure = (error: unknown) =>
+ error instanceof TopicsProviderUnavailable &&
+ (error.reason === "invalid_output" || error.reason === "invalid_input");
async function saveProgress(
metrics: TopicMetrics,
@@ -103,44 +102,95 @@ async function saveProgress(
);
}
+type TraceResults = Map<TraceFacet, TopicSummary | Error>;
+type TraceInput = Awaited<ReturnType<typeof loadTopicTranscript>>;
+
+/**
+ * Summarizes a trace's pending facets with the trace-batch request: one call for
+ * every facet that has no accepted or reusable summary. Results follow `facets`;
+ * a trace-level failure applies only to unresolved facets, execution-level failures throw.
+ */
async function summarizeTrace(
metrics: TopicMetrics,
models: TopicsModels,
execution: ProcessExecution,
- facet: TopicFacetVersion,
+ facets: TraceFacet[],
traceId: string,
- staged: TopicSummary[],
- getTranscript: () => ReturnType<typeof loadTopicTranscript>,
-): Promise<TopicSummary> {
- const accepted = staged.find((row) => row.traceId === traceId);
- if (accepted) {
+ staged: Map<TopicFacetVersion, TopicSummary[]>,
+ getTranscript: () => Promise<TraceInput>,
+): Promise<(TopicSummary | Error)[]> {
+ const results = acceptedSummaries(metrics, facets, traceId, staged);
+ const unresolved = () => facets.filter((facet) => !results.has(facet));
+ if (unresolved().length > 0) {
+ try {
+ const input = await getTranscript();
+ if (isInternalEvalEnvironment(input.environment))
+ throw new Error("Topics does not process internal Langfuse traces.");
+ const base = (facet: TraceFacet) =>
+ baseSummary(execution, traceId, facet.version, input);
+ // Each step records a facet as soon as it is staged, so a later failure keeps it.
+ await reuseSummaries(metrics, execution, unresolved(), base, results);
+ await summarizePending(
+ metrics,
+ models,
+ execution,
+ traceId,
+ unresolved(),
+ base,
+ input,
+ results,
+ );
+ } catch (error) {
+ if (isExecutionFailure(error) && !isTraceModelFailure(error)) throw error;
+ metrics.error("summary", error);
+ for (const facet of unresolved()) results.set(facet, error as Error);
+ }
+ }
+ return facets.map((facet) => results.get(facet)!);
+}
+
+/** Summaries an earlier attempt of this execution already staged. */
+function acceptedSummaries(
+ metrics: TopicMetrics,
+ facets: TraceFacet[],
+ traceId: string,
+ staged: Map<TopicFacetVersion, TopicSummary[]>,
+): TraceResults {
+ const results: TraceResults = new Map();
+ for (const facet of facets) {
+ const accepted = staged
+ .get(facet.version)!
+ .find((row) => row.traceId === traceId);
+ if (!accepted) continue;
if (isInternalEvalEnvironment(accepted.environment))
throw new Error("Topics does not process internal Langfuse traces.");
metrics.result("summary", "cached");
// An accepted payload may expire after the cache read; never renew its TTL.
- return accepted;
+ results.set(facet, accepted);
}
- const { unitStartTime, sessionId, environment, traceName, transcript } =
- await getTranscript();
- if (isInternalEvalEnvironment(environment))
- throw new Error("Topics does not process internal Langfuse traces.");
- const source = {
+ return results;
+}
+
+function baseSummary(
+ execution: ProcessExecution,
+ traceId: string,
+ facet: TopicFacetVersion,
+ input: TraceInput,
+): TopicSummary {
+ return {
projectId: execution.projectId,
facetId: facet.facetId,
facetVersion: facet.version,
traceId,
- sessionId,
- };
- let summary: TopicSummary = {
- ...source,
+ sessionId: input.sessionId,
triggerType: "manual_poc",
- unitStartTime,
- environment,
- traceName,
+ unitStartTime: input.unitStartTime,
+ environment: input.environment,
+ traceName: input.traceName,
state: "insufficient_input",
summary: "",
embedding: [],
- transcriptId: "poc",
+ transcriptId: ASSEMBLED_TRANSCRIPT_ID,
transcriptVersion: TOPICS_TRANSCRIPT_VERSION,
summaryModel: execution.input.processingConfig.summaryModel!,
embeddingModel: execution.input.embeddingConfig.embeddingModel!,
@@ -149,26 +199,38 @@ async function summarizeTrace(
providedCostDetails: {},
costDetails: {},
processedAt: new Date().toISOString(),
- metadata: {},
+ metadata: { executionId: execution.id },
};
- const reusable = execution.input.reuseExistingSummaries
- ? (
- await listTopicSummaries(
- execution.projectId,
- {
- facetId: facet.facetId,
- facetVersion: facet.version,
- traceIds: [traceId],
- },
- summaryTimeRange([summary]),
- )
- ).find(
- (row) =>
- row.summaryModel === execution.input.processingConfig.summaryModel &&
- row.transcriptVersion === TOPICS_TRANSCRIPT_VERSION,
+}
+
+/** Stages compatible stored summaries when the execution opted into reuse. */
+async function reuseSummaries(
+ metrics: TopicMetrics,
+ execution: ProcessExecution,
+ facets: TraceFacet[],
+ base: (facet: TraceFacet) => TopicSummary,
+ results: TraceResults,
+): Promise<void> {
+ if (!execution.input.reuseExistingSummaries) return;
+ for (const facet of facets) {
+ const summary = base(facet);
+ const reusable = (
+ await listTopicSummaries(
+ execution.projectId,
+ {
+ facetId: summary.facetId,
+ facetVersion: summary.facetVersion,
+ traceIds: [summary.traceId!],
+ },
+ summaryTimeRange([summary]),
)
- : undefined;
- if (reusable) {
+ ).find(
+ (row) =>
+ row.summaryModel === execution.input.processingConfig.summaryModel &&
+ row.transcriptId === ASSEMBLED_TRANSCRIPT_ID &&
+ row.transcriptVersion === TOPICS_TRANSCRIPT_VERSION,
+ );
+ if (!reusable) continue;
metrics.result("summary", "cached");
const reuseEmbedding =
reusable.state === "complete" &&
@@ -176,66 +238,87 @@ async function summarizeTrace(
execution.input.embeddingConfig.embeddingModel &&
reusable.embedding.length ===
execution.input.embeddingConfig.embeddingDimensions;
- summary = {
- ...summary,
- state:
- reusable.state === "complete" && !reuseEmbedding
- ? "summarized"
- : reusable.state,
- summary: reusable.summary,
- embedding: reuseEmbedding ? reusable.embedding : [],
- metadata: {
- summaryReusedFromProcessedAt: reusable.processedAt,
- ...(reuseEmbedding
- ? { embeddingReusedFromProcessedAt: reusable.processedAt }
- : {}),
- },
- };
- } else if (transcript !== null) {
- const result = await metrics.measure("summary", async () => {
- const result = await summarizeTopicTrace(
- models,
- facet,
- JSON.stringify(transcript),
- execution.input.processingConfig,
- {
- source_trace_id: traceId,
- source_environment: environment,
- execution_id: execution.id,
+ results.set(
+ facet,
+ await stageSummary(metrics, execution, {
+ ...summary,
+ state:
+ reusable.state === "complete" && !reuseEmbedding
+ ? "summarized"
+ : reusable.state,
+ summary: reusable.summary,
+ embedding: reuseEmbedding ? reusable.embedding : [],
+ metadata: {
+ ...summary.metadata,
+ summaryReusedFromProcessedAt: reusable.processedAt,
+ ...(reuseEmbedding
+ ? { embeddingReusedFromProcessedAt: reusable.processedAt }
+ : {}),
},
- );
- const error = topicSummaryOutputError(result.output);
- if (error) throw invalidOutput(metrics, "summary", error);
- return result;
- });
- metrics.result(
- "summary",
- result.output.status === "applicable"
- ? "generated"
- : result.output.status,
+ }),
);
- summary = {
- ...summary,
- state:
- result.output.status === "applicable"
- ? "summarized"
- : result.output.status,
- summary:
- result.output.status === "applicable"
- ? result.output.summary.trim()
- : "",
- providedUsageDetails: result.providedUsageDetails,
- usageDetails: result.usageDetails,
- providedCostDetails: result.providedCostDetails,
- costDetails: result.costDetails,
- };
- } else {
- metrics.result("summary", "insufficient_input");
}
- return stageSummary(metrics, execution, {
- ...summary,
- metadata: { ...summary.metadata, executionId: execution.id },
+}
+
+/** Summarizes the remaining facets in one shared call and stages each result. */
+async function summarizePending(
+ metrics: TopicMetrics,
+ models: TopicsModels,
+ execution: ProcessExecution,
+ traceId: string,
+ facets: TraceFacet[],
+ base: (facet: TraceFacet) => TopicSummary,
+ input: TraceInput,
+ results: TraceResults,
+): Promise<void> {
+ if (facets.length === 0) return;
+ const summarized = await summarizeTraceFacets({
+ models,
+ facets,
+ transcript: input.transcript,
+ config: execution.input.processingConfig,
+ metadata: {
+ source_trace_id: traceId,
+ source_environment: input.environment,
+ execution_id: execution.id,
+ },
+ metrics,
});
+ const rows: { facet: TraceFacet; summary: TopicSummary }[] = [];
+ for (const [index, facet] of facets.entries()) {
+ const result = summarized?.results[index];
+ if (result && "error" in result) {
+ // An invalid facet output fails only this trace, not the execution.
+ results.set(facet, new Error(result.error.message));
+ continue;
+ }
+ const output = result?.output;
+ if (!output) metrics.result("summary", "insufficient_input");
+ else
+ metrics.result(
+ "summary",
+ output.status === "applicable" ? "generated" : output.status,
+ );
+ rows.push({
+ facet,
+ summary: {
+ ...base(facet),
+ ...(output && {
+ state: output.status === "applicable" ? "summarized" : output.status,
+ summary: output.status === "applicable" ? output.summary.trim() : "",
+ }),
+ },
+ });
+ }
+ // One call summarized every facet; each staged row carries an equal share of its usage.
+ const shares = summarized
+ ? splitTopicModelUsage(summarized.usage, rows.length)
+ : [];
+ for (const [index, { facet, summary }] of rows.entries())
+ results.set(
+ facet,
+ await stageSummary(metrics, execution, { ...summary, ...shares[index] }),
+ );
}
async function stageSummary(
@@ -774,12 +857,15 @@ function compatibleMap(
type PendingFacet = {
facet: TopicFacetVersion;
progress: TopicFacetProgress;
+ name: string;
+ builtIn: boolean;
};
async function pendingFacets(
execution: TopicExecution,
): Promise<PendingFacet[]> {
const facets: PendingFacet[] = [];
+ const definitions = await listTopicFacets(execution.projectId);
for (const progress of execution.facets) {
if (progress.outcome !== "pending") continue;
const facet = await getTopicFacetVersion(
@@ -787,9 +873,16 @@ async function pendingFacets(
progress.facetId,
progress.facetVersion,
);
- if (!facet) throw new Error("Facet version not found in this project.");
+ const definition = definitions.find(({ id }) => id === progress.facetId);
+ if (!facet || !definition)
+ throw new Error("Facet version not found in this project.");
progress.counts.failed = 0;
- facets.push({ facet, progress });
+ facets.push({
+ facet,
+ progress,
+ name: definition.name,
+ builtIn: definition.isBuiltIn,
+ });
}
return facets;
}
@@ -807,7 +900,8 @@ async function processTraces(
const facets: (PendingFacet & {
run: TopicRun | null;
})[] = [];
- for (const { facet, progress } of await pendingFacets(execution)) {
+ for (const pending of await pendingFacets(execution)) {
+ const { facet, progress } = pending;
progress.counts.failed =
state.failedTraceIds.find(
(entry) =>
@@ -821,7 +915,7 @@ async function processTraces(
(run?.status !== "completed" || !compatibleMap(execution, facet, run))
)
throw new Error("The selected topic map is unavailable or incompatible.");
- facets.push({ facet, progress, run });
+ facets.push({ ...pending, run });
}
if (!facets.length) return;
if (!state.summarized) {
@@ -849,7 +943,7 @@ async function processTraces(
)
throw new TopicEmbeddingFailure(TOPIC_EMBEDDING_EXPIRED_ERROR);
}
- // Traces run concurrently; a trace's facets share its transcript and run in order.
+ // Traces run concurrently; a trace's pending facets share one transcript and summary call.
const limit = pLimit(env.LANGFUSE_TOPICS_TRACE_CONCURRENCY);
let saving = Promise.resolve();
// Saves write the whole state, so they must not land out of order.
@@ -877,57 +971,58 @@ async function processTraces(
);
return input;
};
- for (const { facet, progress } of facets) {
- if (
- state.summaries.some(
+ const todo = facets.filter(
+ ({ facet }) =>
+ !state.summaries.some(
(ref) =>
ref.facetId === facet.facetId &&
ref.facetVersion === facet.version &&
ref.traceId === traceId,
- ) ||
- state.failedTraceIds.some(
+ ) &&
+ !state.failedTraceIds.some(
(entry) =>
entry.facetId === facet.facetId &&
entry.facetVersion === facet.version &&
entry.traceIds.includes(traceId),
- )
- )
- continue;
- try {
- await summarizeTrace(
- metrics,
- models,
- execution,
- facet,
- traceId,
- cached.get(facet)!,
- getTranscript,
- );
- state.summaries.push({
- facetId: facet.facetId,
- facetVersion: facet.version,
- traceId,
- });
- await saveInOrder();
- } catch (error) {
- metrics.error("summary", error);
- if (isExecutionFailure(error)) {
- aborted = true;
- throw error;
- }
- recordTraceFailure(
- state,
- progress,
- {
- facetId: facet.facetId,
- facetVersion: facet.version,
- traceId,
- },
- error,
- );
- await saveInOrder();
+ ),
+ );
+ if (todo.length === 0) return;
+ let outcomes: (TopicSummary | Error)[];
+ try {
+ outcomes = await summarizeTrace(
+ metrics,
+ models,
+ execution,
+ todo.map(({ facet, name, builtIn }) => ({
+ version: facet,
+ name,
+ builtIn,
+ })),
+ traceId,
+ cached,
+ getTranscript,
+ );
+ } catch (error) {
+ metrics.error("summary", error);
+ if (isExecutionFailure(error) && !isTraceModelFailure(error)) {
+ aborted = true;
+ throw error;
}
+ outcomes = todo.map(() => error as Error);
+ }
+ for (const [index, { facet, progress }] of todo.entries()) {
+ const outcome = outcomes[index];
+ const ref = {
+ facetId: facet.facetId,
+ facetVersion: facet.version,
+ traceId,
+ };
+ // Invalid facet outputs are already counted by the shared summary call.
+ if (outcome instanceof Error)
+ recordTraceFailure(state, progress, ref, outcome);
+ else state.summaries.push(ref);
}
+ await saveInOrder();
}),
),
);
diff --git a/worker/src/features/topics/provider-error.ts b/worker/src/features/topics/provider-error.ts
index 17516d3f0e..777d155270 100644
--- a/worker/src/features/topics/provider-error.ts
+++ b/worker/src/features/topics/provider-error.ts
@@ -63,6 +63,7 @@ export function topicProviderError(
record.name === "AI_NoOutputGeneratedError" ||
record.name === "AI_TypeValidationError" ||
record.name === "AI_JSONParseError" ||
+ record.name === "SyntaxError" ||
record.name === "ZodError"
)
reason = "invalid_output";
diff --git a/worker/src/features/topics/summarizeAssembledTrace.test.ts b/worker/src/features/topics/summarizeAssembledTrace.test.ts
index f158fa2325..3f39edbe7c 100644
--- a/worker/src/features/topics/summarizeAssembledTrace.test.ts
+++ b/worker/src/features/topics/summarizeAssembledTrace.test.ts
@@ -253,7 +253,7 @@ describe("summarizeAssembledTrace", () => {
expect(state.stored).toHaveBeenCalledTimes(3);
});
- it("summarizes and embeds all pending facets in one call each and records their usage once", async () => {
+ it("summarizes and embeds all pending facets in one call each and splits their usage across the rows", async () => {
const issues: TopicFacet = {
...facet,
id: "facet-2",
@@ -289,12 +289,22 @@ describe("summarizeAssembledTrace", () => {
expect(state.summarize).toHaveBeenCalledOnce();
const [intent, issue] = summaries;
expect(intent).toMatchObject({ facetId: "facet-1", state: "complete" });
- expect(intent.usageDetails.summary_input).toBe(20);
expect(issue).toMatchObject({
facetId: "facet-2",
state: "not_applicable",
- usageDetails: {},
});
+ // Integer token shares and cost shares still add up to the call's usage.
+ expect(summaries.map((row) => row.usageDetails.summary_input)).toEqual([
+ 7, 7, 6,
+ ]);
+ expect(
+ summaries.reduce((sum, row) => sum + row.costDetails.summary_output, 0),
+ ).toBeCloseTo(0.00001, 12);
+ // Each row's total stays the sum of its own shares.
+ for (const { usageDetails } of summaries) {
+ const { total, ...parts } = usageDetails;
+ expect(total).toBe(Object.values(parts).reduce((a, b) => a + b, 0));
+ }
});
it("stores a not-applicable result that carries stray text instead of failing the trace", async () => {
diff --git a/worker/src/features/topics/summarizeAssembledTrace.ts b/worker/src/features/topics/summarizeAssembledTrace.ts
index 5010b1eb1d..72ffd8db24 100644
--- a/worker/src/features/topics/summarizeAssembledTrace.ts
+++ b/worker/src/features/topics/summarizeAssembledTrace.ts
@@ -6,6 +6,7 @@ import {
import {
topicProcessingConfigSchema,
type TopicFacet,
+ type TopicProcessingConfig,
type TopicFacetVersion,
type TopicSummary,
} from "@langfuse/shared/topics";
@@ -20,6 +21,7 @@ import { embedTopicSummaries, summarizeTopicTraceFacets } from "./models";
import { TopicsProviderUnavailable } from "./provider-error";
import {
mergeTopicModelUsage,
+ splitTopicModelUsage,
topicSummaryOutputError,
type TopicModelUsage,
} from "./summaryResult";
@@ -35,6 +37,19 @@ const NO_USAGE: TopicModelUsage = {
costDetails: {},
};
+// Rows from this transcript producer are interchangeable between trace-batch and manual processing.
+export const ASSEMBLED_TRANSCRIPT_ID = "trace-batch";
+
+export type TraceFacet = {
+ name: string;
+ version: TopicFacetVersion;
+ builtIn: boolean;
+};
+
+type TraceFacetResult =
+ | { output: FacetOutput }
+ | { error: TopicsProviderUnavailable };
+
type AssembledTraceInput = {
projectId: string;
traceId: string;
@@ -99,11 +114,7 @@ async function summarizeEnabledTrace(
const config = topicProcessingConfigSchema.parse({
summaryModel: models.summary.model,
});
- const pending: {
- name: string;
- version: TopicFacetVersion;
- builtIn: boolean;
- }[] = [];
+ const pending: TraceFacet[] = [];
// Built-in facets first, each group in listing order, so the cached prompt prefix stays stable.
const ordered = [
...facets.filter((facet) => facet.isBuiltIn),
@@ -137,68 +148,31 @@ async function summarizeEnabledTrace(
}
if (pending.length === 0) return { outcome: "unchanged", rows: [] };
- const prepared = prepareAssembledTopicTranscript(input.transcript);
const bases = pending.map(({ version }) =>
baseSummary(input, version, config, embeddingModel),
);
+ const summarized = await summarizeTraceFacets({
+ models,
+ facets: pending,
+ transcript: input.transcript,
+ config,
+ metadata: {
+ source_trace_id: input.traceId,
+ source_environment: input.environment,
+ },
+ metrics,
+ });
let rows = bases;
let failure: unknown;
- if (prepared.hasContent) {
- const keyed = pending.map(({ name, version, builtIn }, index) => ({
- key: `${name.toLowerCase().replace(/[^a-z0-9]+/g, "_")}_${index + 1}`,
- facet: version,
- builtIn,
- }));
- const invalidFacets = new Set<string>();
- let generated:
- | Awaited<ReturnType<typeof summarizeTopicTraceFacets>>
- | undefined;
- const result = await metrics
- .measure("summary", async () => {
- generated = await summarizeTopicTraceFacets(
- models,
- keyed,
- prepared.text,
- config,
- {
- source_trace_id: input.traceId,
- source_environment: input.environment,
- },
- );
- const outputs = generated.output as Record<string, FacetOutput>;
- for (const { key } of keyed) {
- const message = outputs[key]
- ? topicSummaryOutputError(outputs[key])
- : "The model response is missing a facet.";
- if (!message) continue;
- const error = new TopicsProviderUnavailable(
- message,
- "invalid_output",
- );
- invalidFacets.add(key);
- metrics.error("summary", error);
- if (failure === undefined) failure = error;
- }
- if (failure !== undefined) throw failure;
- return generated;
- })
- .catch((error: unknown) => {
- // Valid facets can still be persisted after summary validation fails.
- if (error !== failure || !generated) throw error;
- return generated;
- });
- const outputs = result.output as Record<string, FacetOutput>;
- const usage: TopicModelUsage = {
- providedUsageDetails: result.providedUsageDetails,
- usageDetails: result.usageDetails,
- providedCostDetails: result.providedCostDetails,
- costDetails: result.costDetails,
- };
- const accepted = keyed.flatMap(({ key }, index) =>
- invalidFacets.has(key)
- ? []
- : [{ base: bases[index], output: outputs[key] }],
- );
+ if (summarized) {
+ const usage = summarized.usage;
+ const accepted = summarized.results.flatMap((result, index) => {
+ if ("error" in result) {
+ if (failure === undefined) failure = result.error;
+ return [];
+ }
+ return [{ base: bases[index], output: result.output }];
+ });
const applicable = accepted
.filter(({ output }) => output.status === "applicable")
.map(({ output }) => output.summary.trim());
@@ -217,34 +191,110 @@ async function summarizeEnabledTrace(
if (failure === undefined) failure = error;
}
}
- // One call summarizes and one embeds every facet of the trace; record their usage once.
+ // One call summarizes and one embeds every facet of the trace; each row carries an equal share.
const traceUsage = embedded ? mergeTopicModelUsage(usage, embedded) : usage;
- rows = [];
+ const generated: TopicSummary[] = [];
let embeddingIndex = 0;
for (const { base, output } of accepted) {
- const rowUsage = rows.length > 0 ? NO_USAGE : traceUsage;
if (output.status !== "applicable")
- rows.push({
+ generated.push({
...base,
state:
output.status === "not_applicable"
? "not_applicable"
: "insufficient_input",
- ...rowUsage,
});
else if (embedded)
- rows.push({
+ generated.push({
...base,
state: "complete",
summary: output.summary.trim(),
embedding: embedded.embeddings[embeddingIndex++],
- ...rowUsage,
});
}
+ const shares = splitTopicModelUsage(traceUsage, generated.length);
+ rows = generated.map((row, index) => ({ ...row, ...shares[index] }));
}
return { outcome: "summarized", rows, failure };
}
+/**
+ * Summarizes the given facets of one trace in a single model call on the
+ * projected transcript. Trace-batch and manual processing share this request.
+ * Returns undefined without a call when the transcript has no content; results
+ * follow the order of `facets`, with an error for each invalid facet output.
+ */
+export async function summarizeTraceFacets(params: {
+ models: TopicsModels;
+ facets: TraceFacet[];
+ transcript: Transcript | null;
+ config: TopicProcessingConfig;
+ metadata: Record<string, unknown>;
+ metrics: TopicMetrics;
+}): Promise<
+ { results: TraceFacetResult[]; usage: TopicModelUsage } | undefined
+> {
+ const { models, facets, config, metrics } = params;
+ const prepared = prepareAssembledTopicTranscript(params.transcript);
+ if (!prepared.hasContent) return undefined;
+ // Built-in facets first, each group in listing order, so the cached prompt prefix stays stable.
+ const ordered = [
+ ...facets.filter((facet) => facet.builtIn),
+ ...facets.filter((facet) => !facet.builtIn),
+ ];
+ const keyed = ordered.map(({ name, version, builtIn }, index) => ({
+ key: `${name.toLowerCase().replace(/[^a-z0-9]+/g, "_")}_${index + 1}`,
+ facet: version,
+ builtIn,
+ }));
+ const errors = new Map<string, TopicsProviderUnavailable>();
+ let generated:
+ | Awaited<ReturnType<typeof summarizeTopicTraceFacets>>
+ | undefined;
+ await metrics
+ .measure("summary", async () => {
+ generated = await summarizeTopicTraceFacets(
+ models,
+ keyed,
+ prepared.text,
+ config,
+ params.metadata,
+ );
+ const outputs = generated.output as Record<string, FacetOutput>;
+ for (const { key } of keyed) {
+ const message = outputs[key]
+ ? topicSummaryOutputError(outputs[key])
+ : "The model response is missing a facet.";
+ if (!message) continue;
+ const error = new TopicsProviderUnavailable(message, "invalid_output");
+ errors.set(key, error);
+ metrics.error("summary", error);
+ }
+ const [first] = errors.values();
+ if (first) throw first;
+ })
+ .catch((error: unknown) => {
+ // Valid facets can still be persisted after summary validation fails.
+ if (!generated || ![...errors.values()].includes(error as never))
+ throw error;
+ });
+ const result = generated!;
+ const outputs = result.output as Record<string, FacetOutput>;
+ return {
+ results: facets.map((facet) => {
+ const { key } = keyed[ordered.indexOf(facet)];
+ const error = errors.get(key);
+ return error ? { error } : { output: outputs[key] };
+ }),
+ usage: {
+ providedUsageDetails: result.providedUsageDetails,
+ usageDetails: result.usageDetails,
+ providedCostDetails: result.providedCostDetails,
+ costDetails: result.costDetails,
+ },
+ };
+}
+
function baseSummary(
input: {
projectId: string;
@@ -270,7 +320,7 @@ function baseSummary(
state: "insufficient_input",
summary: "",
embedding: [],
- transcriptId: "trace-batch",
+ transcriptId: ASSEMBLED_TRANSCRIPT_ID,
transcriptVersion: TOPICS_TRANSCRIPT_VERSION,
summaryModel: config.summaryModel!,
embeddingModel,
diff --git a/worker/src/features/topics/summaryResult.ts b/worker/src/features/topics/summaryResult.ts
index 78ba0d4d07..1425801766 100644
--- a/worker/src/features/topics/summaryResult.ts
+++ b/worker/src/features/topics/summaryResult.ts
@@ -49,3 +49,56 @@ export function mergeTopicModelUsage(
costDetails: mergeDetails(summary.costDetails, embedding.costDetails),
};
}
+
+// Token counts are stored as integers and costs with 12 decimals.
+const COST_UNITS = 1e12;
+
+function splitDetails(
+ details: Record<string, number>,
+ count: number,
+ units: number,
+): Record<string, number>[] {
+ const shares = Array.from(
+ { length: count },
+ () => ({}) as Record<string, number>,
+ );
+ const { total, ...parts } = details;
+ const split = (key: string, value: number) => {
+ const scaled = Math.round(value * units);
+ const base = Math.floor(scaled / count);
+ // The remainder goes to the first rows, so the shares add up to the value.
+ shares.forEach((share, index) => {
+ share[key] = (base + (index < scaled - base * count ? 1 : 0)) / units;
+ });
+ };
+ for (const [key, value] of Object.entries(parts)) split(key, value);
+ if (total === undefined) return shares;
+ const unit = (value: number) => Math.round(value * units);
+ const sum = (values: number[]) => values.reduce((a, b) => a + unit(b), 0);
+ // A total that is the sum of its parts stays the sum of each row's own parts.
+ if (unit(total) === sum(Object.values(parts)))
+ for (const share of shares) share.total = sum(Object.values(share)) / units;
+ else split("total", total);
+ return shares;
+}
+
+/** Splits one call's usage evenly across the rows it produced. */
+export function splitTopicModelUsage(
+ usage: TopicModelUsage,
+ count: number,
+): TopicModelUsage[] {
+ const providedUsage = splitDetails(usage.providedUsageDetails, count, 1);
+ const usageDetails = splitDetails(usage.usageDetails, count, 1);
+ const providedCost = splitDetails(
+ usage.providedCostDetails,
+ count,
+ COST_UNITS,
+ );
+ const costDetails = splitDetails(usage.costDetails, count, COST_UNITS);
+ return Array.from({ length: count }, (_, index) => ({
+ providedUsageDetails: providedUsage[index],
+ usageDetails: usageDetails[index],
+ providedCostDetails: providedCost[index],
+ costDetails: costDetails[index],
+ }));
+}