Skip to content

Commit 3c16e46

Browse files
authored
Merge pull request #6722 from continuedev/nate/anthropic-stop-reason
fix: increase default max_tokens for Anthropic API to 32k
2 parents 4f78352 + 5b09028 commit 3c16e46

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/openai-adapters/src/apis/Anthropic.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ export class AnthropicApi implements BaseLlmApi {
5555
return cachingStrategy(cleanBody);
5656
}
5757

58+
private maxTokensForModel(model: string): number {
59+
if (model.includes("haiku")) {
60+
return 8192;
61+
}
62+
63+
return 32_000;
64+
}
65+
5866
public _convertToCleanAnthropicBody(oaiBody: ChatCompletionCreateParams) {
5967
let stop = undefined;
6068
if (oaiBody.stop && Array.isArray(oaiBody.stop)) {
@@ -81,7 +89,7 @@ export class AnthropicApi implements BaseLlmApi {
8189
: systemMessage,
8290
top_p: oaiBody.top_p,
8391
temperature: oaiBody.temperature,
84-
max_tokens: oaiBody.max_tokens ?? 4096, // max_tokens is required
92+
max_tokens: oaiBody.max_tokens ?? this.maxTokensForModel(oaiBody.model), // max_tokens is required
8593
model: oaiBody.model,
8694
stop_sequences: stop,
8795
stream: oaiBody.stream,

packages/openai-adapters/src/test/anthropic-adapter.vitest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, test, vi, expect, afterEach } from "vitest";
1+
import { afterEach, describe, expect, test, vi } from "vitest";
22
import { runAdapterTest } from "./adapter-test-utils.js";
33

44
// Mock the fetch package
@@ -53,7 +53,7 @@ describe("Anthropic Adapter Tests", () => {
5353
messages: [{ role: "user", content: "hello" }],
5454
system: undefined,
5555
model: "claude-3-5-sonnet-20241022",
56-
max_tokens: 4096,
56+
max_tokens: 32000,
5757
stream: undefined,
5858
},
5959
},
@@ -107,7 +107,7 @@ describe("Anthropic Adapter Tests", () => {
107107
messages: [{ role: "user", content: "hello" }],
108108
system: undefined,
109109
model: "claude-3-5-sonnet-20241022",
110-
max_tokens: 4096,
110+
max_tokens: 32000,
111111
stream: true,
112112
},
113113
},
@@ -168,7 +168,7 @@ describe("Anthropic Adapter Tests", () => {
168168
},
169169
],
170170
model: "claude-3-5-sonnet-20241022",
171-
max_tokens: 4096,
171+
max_tokens: 32000,
172172
stream: true,
173173
},
174174
},

0 commit comments

Comments
 (0)