Skip to content

Commit 1899e68

Browse files
committed
refactor(chat): prune context
1 parent 3a3ad9b commit 1899e68

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

app/api/chat/route.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getAuthorizedUserInfo } from "@/lib/auth.rsc";
44
import { createPostHogClient } from "@/lib/posthog.rsc";
55
import { anthropic, type AnthropicProviderOptions } from "@ai-sdk/anthropic";
66
import { withTracing } from "@posthog/ai";
7-
import { convertToModelMessages, stepCountIs, streamText, tool, type UIMessage } from "ai";
7+
import { convertToModelMessages, pruneMessages, stepCountIs, streamText, tool, type UIMessage } from "ai";
88
import { NextResponse } from "next/server";
99
import { z } from "zod";
1010

@@ -106,11 +106,18 @@ export async function POST(req: Request) {
106106
posthogDistinctId: userInfo.sub,
107107
});
108108

109+
const prunedMessages = pruneMessages({
110+
messages: convertToModelMessages(messages),
111+
reasoning: 'none',
112+
toolCalls: 'before-last-message',
113+
emptyMessages: 'remove',
114+
});
115+
109116
const result = streamText({
110117
model: tracedModel,
111118
providerOptions: {
112119
anthropic: {
113-
thinking: { type: "enabled", budgetTokens: 12000 },
120+
thinking: { type: "enabled", budgetTokens: 2000 },
114121
} satisfies AnthropicProviderOptions,
115122
},
116123
messages: [
@@ -134,7 +141,7 @@ export async function POST(req: Request) {
134141
} satisfies AnthropicProviderOptions,
135142
},
136143
},
137-
...convertToModelMessages(messages),
144+
...prunedMessages,
138145
],
139146
stopWhen: stepCountIs(10),
140147
tools: {

0 commit comments

Comments
 (0)