Skip to content

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

chore(in-app agent): Improve 'thinking' chunks instrumentation (#14971)

Lysander Kiesel70457acfbd21
diff --git a/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts b/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts
index 91166efb77..a01210c08b 100644
--- a/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts
+++ b/web/src/__tests__/server/unit/in-app-agent-instrumentation.servertest.ts
@@ -689,10 +689,21 @@ describe("InAppAgentInstrumentation", () => {
);
});
- it("records reasoning text in agent generation metadata", () => {
+ it("records reasoning as thinking parts on the assistant output messages", () => {
const instrumentation = createInstrumentation();
+ const toolCall = {
+ id: "tool-1",
+ name: "listObservations",
+ arguments: '{"limit":10}',
+ type: "function",
+ };
+ const toolOutput = { traces: [{ id: "trace-1" }] };
instrumentation.recordEvents([
+ {
+ type: EventType.REASONING_MESSAGE_START,
+ messageId: "reasoning-1",
+ },
{
type: EventType.REASONING_MESSAGE_CONTENT,
messageId: "reasoning-1",
@@ -703,6 +714,42 @@ describe("InAppAgentInstrumentation", () => {
messageId: "reasoning-1",
delta: "filters",
},
+ {
+ type: EventType.REASONING_MESSAGE_END,
+ messageId: "reasoning-1",
+ },
+ {
+ type: EventType.TOOL_CALL_START,
+ toolCallId: "tool-1",
+ toolCallName: "listObservations",
+ },
+ {
+ type: EventType.TOOL_CALL_ARGS,
+ toolCallId: "tool-1",
+ delta: '{"limit":10}',
+ },
+ {
+ type: EventType.TOOL_CALL_END,
+ toolCallId: "tool-1",
+ },
+ {
+ type: EventType.TOOL_CALL_RESULT,
+ toolCallId: "tool-1",
+ content: JSON.stringify(toolOutput),
+ },
+ {
+ type: EventType.REASONING_MESSAGE_START,
+ messageId: "reasoning-2",
+ },
+ {
+ type: EventType.REASONING_MESSAGE_CONTENT,
+ messageId: "reasoning-2",
+ delta: "Drafting the answer",
+ },
+ {
+ type: EventType.REASONING_MESSAGE_END,
+ messageId: "reasoning-2",
+ },
{
type: EventType.TEXT_MESSAGE_CONTENT,
delta: "Done",
@@ -717,11 +764,58 @@ describe("InAppAgentInstrumentation", () => {
name: "agent-turn",
input: expectedAgentRunInput,
output: {
- messages: [{ role: "assistant", content: "Done" }],
+ messages: [
+ {
+ role: "assistant",
+ content: "",
+ thinking: [{ type: "thinking", content: "Checking filters" }],
+ tool_calls: [toolCall],
+ },
+ { role: "tool", tool_call_id: "tool-1", content: toolOutput },
+ {
+ role: "assistant",
+ content: "Done",
+ thinking: [{ type: "thinking", content: "Drafting the answer" }],
+ },
+ ],
text: "Done",
+ tool_calls: [toolCall],
},
completionStartTime: expect.any(Date),
- metadata: expect.objectContaining({ reasoning: "Checking filters" }),
+ }),
+ );
+ const updateArg = mocks.agentGeneration.update.mock.calls.at(-1)?.[0] as {
+ metadata?: Record<string, unknown>;
+ };
+ expect(updateArg.metadata).not.toHaveProperty("reasoning");
+ });
+
+ it("records reasoning without a following assistant message as its own thinking message", () => {
+ const instrumentation = createInstrumentation();
+
+ instrumentation.recordEvents([
+ {
+ type: EventType.REASONING_MESSAGE_CONTENT,
+ messageId: "reasoning-1",
+ delta: "Checking filters",
+ },
+ {
+ type: EventType.RUN_FINISHED,
+ },
+ ]);
+
+ expect(mocks.agentGeneration.update).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name: "agent-turn",
+ output: {
+ messages: [
+ {
+ role: "assistant",
+ content: "",
+ thinking: [{ type: "thinking", content: "Checking filters" }],
+ },
+ ],
+ },
}),
);
});
diff --git a/web/src/ee/features/in-app-agent/server/instrumentation.ts b/web/src/ee/features/in-app-agent/server/instrumentation.ts
index d0b33074f6..0e59d3b00e 100644
--- a/web/src/ee/features/in-app-agent/server/instrumentation.ts
+++ b/web/src/ee/features/in-app-agent/server/instrumentation.ts
@@ -64,10 +64,18 @@ type AgentRunSkillDefinition = {
name: string;
description?: string;
};
+// Matches the ChatML thinking content part
+// (packages/shared/src/utils/IORepresentation/chatML/types.ts) so the trace UI
+// renders reasoning as collapsible "Thinking" blocks.
+type AgentRunThinkingPart = {
+ type: "thinking";
+ content: string;
+};
type AgentRunChatMessage = {
role: string;
name?: string;
content?: unknown;
+ thinking?: AgentRunThinkingPart[];
tool_calls?: AgentRunToolCall[];
tool_call_id?: string;
};
@@ -140,7 +148,8 @@ export class InAppAgentInstrumentation {
private readonly agentRunOutputMessages: AgentRunChatMessage[] = [];
private readonly agentRunToolCalls: AgentRunToolCall[] = [];
private output = "";
- private reasoning = "";
+ private currentReasoning = "";
+ private pendingThinking: AgentRunThinkingPart[] = [];
private completionStartTime?: Date;
private ended = false;
@@ -274,6 +283,7 @@ export class InAppAgentInstrumentation {
const message = error instanceof Error ? error.message : String(error);
this.endOpenToolSpans({ error: message }, message);
+ this.flushTrailingThinking();
this.agentRun.update({
name: IN_APP_AGENT_TURN_NAME,
input: this.agentRunInput,
@@ -285,7 +295,6 @@ export class InAppAgentInstrumentation {
statusMessage: message,
metadata: {
...this.metadata,
- ...(this.reasoning ? { reasoning: this.reasoning } : {}),
error: message,
},
...(this.prompt
@@ -310,9 +319,9 @@ export class InAppAgentInstrumentation {
}
this.endOpenToolSpans(params?.aborted ? { aborted: true } : undefined);
+ this.flushTrailingThinking();
const metadata = {
...this.metadata,
- ...(this.reasoning ? { reasoning: this.reasoning } : {}),
...(params?.aborted ? { aborted: true } : {}),
...(params?.result ? { result: params.result } : {}),
};
@@ -362,11 +371,19 @@ export class InAppAgentInstrumentation {
event.type === EventType.REASONING_MESSAGE_CONTENT
) {
if (typeof event.delta === "string") {
- this.reasoning += event.delta;
+ this.currentReasoning += event.delta;
}
return;
}
+ if (
+ event.type === EventType.REASONING_MESSAGE_START ||
+ event.type === EventType.REASONING_MESSAGE_END
+ ) {
+ this.flushCurrentReasoning();
+ return;
+ }
+
if (event.type === EventType.TOOL_CALL_START) {
this.startToolSpan(event);
return;
@@ -416,8 +433,6 @@ export class InAppAgentInstrumentation {
event.type === EventType.STEP_FINISHED ||
event.type === EventType.TOOL_CALL_CHUNK ||
event.type === EventType.REASONING_START ||
- event.type === EventType.REASONING_MESSAGE_START ||
- event.type === EventType.REASONING_MESSAGE_END ||
event.type === EventType.REASONING_END ||
event.type === EventType.REASONING_ENCRYPTED_VALUE ||
// eslint-disable-next-line @typescript-eslint/no-deprecated
@@ -582,12 +597,56 @@ export class InAppAgentInstrumentation {
}
}
+ // Reasoning that no assistant message followed (e.g. aborted or errored
+ // turns) still gets recorded as its own thinking-only assistant message.
+ private flushTrailingThinking() {
+ const thinking = this.takePendingThinking();
+ if (!thinking) {
+ return;
+ }
+
+ this.agentRunOutputMessages.push({
+ role: "assistant",
+ content: "",
+ thinking,
+ });
+ }
+
+ private flushCurrentReasoning() {
+ if (!this.currentReasoning) {
+ return;
+ }
+
+ this.pendingThinking.push({
+ type: "thinking",
+ content: this.currentReasoning,
+ });
+ this.currentReasoning = "";
+ }
+
+ // Reasoning always precedes the assistant action it produced, so pending
+ // thinking parts attach to the next assistant output message.
+ private takePendingThinking(): AgentRunThinkingPart[] | undefined {
+ this.flushCurrentReasoning();
+
+ if (this.pendingThinking.length === 0) {
+ return undefined;
+ }
+
+ return this.pendingThinking.splice(0);
+ }
+
private recordAssistantText(delta: string) {
this.output += delta;
this.completionStartTime ??= new Date();
+ const thinking = this.takePendingThinking();
const lastMessage = this.agentRunOutputMessages.at(-1);
- if (lastMessage?.role === "assistant" && !lastMessage.tool_calls?.length) {
+ if (
+ !thinking &&
+ lastMessage?.role === "assistant" &&
+ !lastMessage.tool_calls?.length
+ ) {
lastMessage.content = `${typeof lastMessage.content === "string" ? lastMessage.content : ""}${delta}`;
return;
}
@@ -595,6 +654,7 @@ export class InAppAgentInstrumentation {
this.agentRunOutputMessages.push({
role: "assistant",
content: delta,
+ ...(thinking ? { thinking } : {}),
});
}
@@ -616,10 +676,12 @@ export class InAppAgentInstrumentation {
type: "function",
};
+ const thinking = this.takePendingThinking();
this.agentRunToolCalls.push(toolCall);
this.agentRunOutputMessages.push({
role: "assistant",
content: "",
+ ...(thinking ? { thinking } : {}),
tool_calls: [toolCall],
});