Skip to content

Commit e78e846

Browse files
committed
fix(kiro): use provider token ratio for GPT routes
1 parent 7e21e64 commit e78e846

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎src/adapters/kiro.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ function estimateKiroImageTokens(image: KiroImage): number {
144144
return Math.max(256, Math.ceil(decodedBytes / 512));
145145
}
146146

147+
function estimateKiroTokens(text: string, modelId?: string): number {
148+
return estimateTokens(text, modelId ? `kiro/${modelId}` : "kiro");
149+
}
150+
147151
function estimateKiroPayloadInputTokens(payload: Record<string, unknown>, modelId: string): number {
148152
const conversationState = (payload as {
149153
conversationState?: {
@@ -174,7 +178,7 @@ function estimateKiroPayloadInputTokens(payload: Record<string, unknown>, modelI
174178
if (assistant.toolUses?.length) parts.push(serializeForUsage(assistant.toolUses));
175179
}
176180
}
177-
return estimateTokens(parts.join("\n"), modelId) + imageTokens;
181+
return estimateKiroTokens(parts.join("\n"), modelId) + imageTokens;
178182
}
179183

180184
function shouldCountStablePromptOverhead(parsed: OcxParsedRequest): boolean {
@@ -191,14 +195,14 @@ function estimateKiroInputTokens(parsed: OcxParsedRequest): number {
191195
if (parsed.context.tools?.length) parts.push(serializeForUsage(parsed.context.tools));
192196
}
193197

194-
return estimateTokens(parts.join("\n"), parsed.modelId);
198+
return estimateKiroTokens(parts.join("\n"), parsed.modelId);
195199
}
196200

197201
function estimateKiroLogInputTokens(parsed: OcxParsedRequest): number {
198202
const parts = parsed.context.messages.map(messageLogText).filter(Boolean);
199203
if (parsed.context.systemPrompt?.length) parts.push(...parsed.context.systemPrompt);
200204
if (parsed.context.tools?.length) parts.push(serializeForUsage(parsed.context.tools));
201-
return Math.max(estimateKiroInputTokens(parsed), estimateTokens(parts.join("\n"), parsed.modelId));
205+
return Math.max(estimateKiroInputTokens(parsed), estimateKiroTokens(parts.join("\n"), parsed.modelId));
202206
}
203207

204208
function kiroUpstreamContextWindow(modelId: string | undefined): number | undefined {
@@ -631,7 +635,7 @@ async function* parseKiroAttempt(
631635
const usage = (): OcxUsage => {
632636
const base = authoritativeUsage ?? {
633637
inputTokens,
634-
outputTokens: estimateTokens(outputChars, modelId),
638+
outputTokens: estimateKiroTokens(outputChars, modelId),
635639
estimated: true,
636640
};
637641
const estimatedContextTotal = contextInputEstimate !== undefined

‎tests/kiro-stream.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,16 @@ describe("kiro adapter — parseStream", () => {
953953
expect(done.contextTotalTokens).toBe(50_000);
954954
});
955955

956+
test("Kiro GPT routes use the Kiro token ratio without context percentage", async () => {
957+
const adapter = createKiroAdapter(provider);
958+
await adapter.buildRequest(parsedWith([{ role: "user", content: "x".repeat(3500) }], undefined, "gpt-5.6-sol"));
959+
const done = await doneUsage(adapter, eventFrame({ content: "y".repeat(3500) }));
960+
961+
expect(done.inputTokens).toBe(1000);
962+
expect(done.outputTokens).toBe(1000);
963+
expect(done.contextTotalTokens).toBe(2000);
964+
});
965+
956966
test("fresh payload includes history while usage counts only the current turn", async () => {
957967
const latest = "please summarize recent commits";
958968
const shortMessages = [

0 commit comments

Comments
 (0)