Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/slim-llm-request-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core": patch
"@moonshot-ai/kimi-code": patch
---

Slim the LLM diagnostic logs with fewer, more compact fields.
62 changes: 22 additions & 40 deletions packages/agent-core/src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,23 @@ export class Agent {
configMetadata,
buildLlmConfigSignature(configMetadata, systemPrompt, tools),
);

let partialMessageCount = 0;
for (const message of history) {
if (message.partial === true) partialMessageCount += 1;
}
const requestMetadata: LlmRequestMetadata = {
estimatedInputTokens:
estimateTokens(systemPrompt) +
estimateTokensForMessages(history) +
estimateTokensForTools(tools),
};
if (partialMessageCount > 0) {
requestMetadata.partialMessageCount = partialMessageCount;
}
this.log.info('llm request', {
...context,
...buildLlmRequestMetadata(systemPrompt, tools, history),
...requestMetadata,
});
}

Expand Down Expand Up @@ -414,16 +428,12 @@ export class Agent {
}

interface LlmRequestContextFields {
turnId?: string;
step?: number;
attempt?: number;
maxAttempts?: number;
turnStep?: string;
attempt?: string;
}

interface LlmRequestMetadata {
estimatedInputTokens: number;
messageCount: number;
toolCallCount: number;
partialMessageCount?: number;
}

Expand All @@ -447,49 +457,21 @@ function buildLlmRequestContext(options: Parameters<typeof generate>[5]): LlmReq
if (context === undefined) return {};

const fields: LlmRequestContextFields = {
turnId: context.turnId,
step: context.step,
turnStep:
context.turnId === undefined || context.step === undefined
? undefined
: `${context.turnId}.${String(context.step)}`,
};
if (
context.attempt !== undefined &&
context.maxAttempts !== undefined &&
context.attempt > 1
) {
fields.attempt = context.attempt;
fields.maxAttempts = context.maxAttempts;
fields.attempt = `${String(context.attempt)}/${String(context.maxAttempts)}`;
}
return fields;
}

function buildLlmRequestMetadata(
systemPrompt: string,
tools: readonly Tool[],
history: readonly Message[],
): LlmRequestMetadata {
let toolCallCount = 0;
let partialMessageCount = 0;

for (const message of history) {
if (message.partial === true) partialMessageCount += 1;
toolCallCount += message.toolCalls.length;
}

const estimatedInputTokens =
estimateTokens(systemPrompt) +
estimateTokensForMessages(history) +
estimateTokensForTools(tools);

const metadata: LlmRequestMetadata = {
estimatedInputTokens,
messageCount: history.length,
toolCallCount,
};
if (partialMessageCount > 0) {
metadata.partialMessageCount = partialMessageCount;
}
return metadata;
}

function buildLlmConfigMetadata(
provider: ChatProvider,
modelAlias: string | undefined,
Expand Down
6 changes: 2 additions & 4 deletions packages/agent-core/src/loop/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ function logRequestFailure(
): void {
if (isAbortError(error) || input.params.signal.aborted) return;
input.log?.warn('llm request failed', {
turnId: input.turnId,
step: input.currentStep,
attempt,
maxAttempts,
turnStep: `${input.turnId}.${String(input.currentStep)}`,
attempt: `${String(attempt)}/${String(maxAttempts)}`,
model: input.llm.modelName,
...retryErrorFields(error),
});
Expand Down
20 changes: 10 additions & 10 deletions packages/agent-core/test/agent/turn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,7 @@ describe('Agent turn flow', () => {
expect(configLogs).toHaveLength(1);
const configPayload = configLogs[0]?.payload as Record<string, unknown>;
expect(configPayload).toMatchObject({
turnId: '0',
step: 1,
turnStep: '0.1',
provider: 'kimi',
model: 'mock-model',
modelAlias: 'mock-model',
Expand All @@ -731,12 +730,11 @@ describe('Agent turn flow', () => {
expect(requestLogs).toHaveLength(1);
const payload = requestLogs[0]?.payload as Record<string, unknown>;
expect(payload).toMatchObject({
turnId: '0',
step: 1,
messageCount: 1,
toolCallCount: 0,
turnStep: '0.1',
});
expect(payload['estimatedInputTokens']).toEqual(expect.any(Number));
expect(payload).not.toHaveProperty('turnId');
expect(payload).not.toHaveProperty('step');
expect(payload).not.toHaveProperty('attempt');
expect(payload).not.toHaveProperty('maxAttempts');
expect(payload).not.toHaveProperty('stepUuid');
Expand All @@ -746,6 +744,8 @@ describe('Agent turn flow', () => {
expect(payload).not.toHaveProperty('thinkingEffort');
expect(payload).not.toHaveProperty('systemPromptChars');
expect(payload).not.toHaveProperty('partialMessageCount');
expect(payload).not.toHaveProperty('messageCount');
expect(payload).not.toHaveProperty('toolCallCount');
expect(payload).not.toHaveProperty('toolCount');
expect(payload).not.toHaveProperty('systemPromptHash');
expect(payload).not.toHaveProperty('toolsHash');
Expand Down Expand Up @@ -1152,10 +1152,10 @@ describe('Agent turn flow', () => {
}),
);
const requestLogs = entries.filter((entry) => entry.message === 'llm request');
expect(requestLogs.map((entry) => entry.payload)).toEqual([
expect.not.objectContaining({ attempt: expect.any(Number), maxAttempts: expect.any(Number) }),
expect.objectContaining({ attempt: 2, maxAttempts: 3 }),
]);
const payloads = requestLogs.map((entry) => entry.payload as Record<string, unknown>);
expect(payloads[0]).toMatchObject({ turnStep: '0.1' });
expect(payloads[0]).not.toHaveProperty('attempt');
expect(payloads[1]).toMatchObject({ turnStep: '0.1', attempt: '2/3' });
});

it('force-refreshes OAuth credentials on video upload 401 and falls back to login_required when replay 401', async () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/agent-core/test/loop/error-paths.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ describe('runTurn — error paths', () => {
level: 'warn',
message: 'llm request failed',
payload: {
turnId: 'turn-1',
step: 1,
attempt: 1,
maxAttempts: 3,
turnStep: 'turn-1.1',
attempt: '1/3',
model: 'fake-model',
errorName: 'Error',
errorMessage: 'upstream blew up',
Expand Down
Loading