Skip to content

Commit 2ceeeff

Browse files
chore(core): extend getModelContextSize to account for more models (#9372)
1 parent bce4e45 commit 2ceeeff

File tree

2 files changed

+98
-7
lines changed

2 files changed

+98
-7
lines changed

libs/langchain-core/src/language_models/base.ts

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ import {
3030
// https://www.npmjs.com/package/js-tiktoken
3131

3232
export const getModelNameForTiktoken = (modelName: string): TiktokenModel => {
33+
if (modelName.startsWith("gpt-5")) {
34+
return "gpt-5" as TiktokenModel;
35+
}
36+
3337
if (modelName.startsWith("gpt-3.5-turbo-16k")) {
3438
return "gpt-3.5-turbo-16k";
3539
}
@@ -62,28 +66,103 @@ export const getEmbeddingContextSize = (modelName?: string): number => {
6266
}
6367
};
6468

69+
/**
70+
* Get the context window size (max input tokens) for a given model.
71+
*
72+
* Context window sizes are sourced from official model documentation:
73+
* - OpenAI: https://platform.openai.com/docs/models
74+
* - Anthropic: https://docs.anthropic.com/claude/docs/models-overview
75+
* - Google: https://ai.google.dev/gemini/docs/models/gemini
76+
*
77+
* @param modelName - The name of the model
78+
* @returns The context window size in tokens
79+
*/
6580
export const getModelContextSize = (modelName: string): number => {
66-
switch (getModelNameForTiktoken(modelName)) {
67-
case "gpt-3.5-turbo-16k":
68-
return 16384;
69-
case "gpt-3.5-turbo":
70-
return 4096;
81+
const normalizedName = getModelNameForTiktoken(modelName) as string;
82+
83+
switch (normalizedName) {
84+
// GPT-5 series
85+
case "gpt-5":
86+
case "gpt-5-turbo":
87+
case "gpt-5-turbo-preview":
88+
return 400000;
89+
90+
// GPT-4o series
91+
case "gpt-4o":
92+
case "gpt-4o-mini":
93+
case "gpt-4o-2024-05-13":
94+
case "gpt-4o-2024-08-06":
95+
return 128000;
96+
97+
// GPT-4 Turbo series
98+
case "gpt-4-turbo":
99+
case "gpt-4-turbo-preview":
100+
case "gpt-4-turbo-2024-04-09":
101+
case "gpt-4-0125-preview":
102+
case "gpt-4-1106-preview":
103+
return 128000;
104+
105+
// GPT-4 series
71106
case "gpt-4-32k":
107+
case "gpt-4-32k-0314":
108+
case "gpt-4-32k-0613":
72109
return 32768;
73110
case "gpt-4":
111+
case "gpt-4-0314":
112+
case "gpt-4-0613":
74113
return 8192;
114+
115+
// GPT-3.5 Turbo series
116+
case "gpt-3.5-turbo-16k":
117+
case "gpt-3.5-turbo-16k-0613":
118+
return 16384;
119+
case "gpt-3.5-turbo":
120+
case "gpt-3.5-turbo-0301":
121+
case "gpt-3.5-turbo-0613":
122+
case "gpt-3.5-turbo-1106":
123+
case "gpt-3.5-turbo-0125":
124+
return 4096;
125+
126+
// Legacy GPT-3 models
75127
case "text-davinci-003":
128+
case "text-davinci-002":
76129
return 4097;
130+
case "text-davinci-001":
131+
return 2049;
77132
case "text-curie-001":
78-
return 2048;
79133
case "text-babbage-001":
80-
return 2048;
81134
case "text-ada-001":
82135
return 2048;
136+
137+
// Code models
83138
case "code-davinci-002":
139+
case "code-davinci-001":
84140
return 8000;
85141
case "code-cushman-001":
86142
return 2048;
143+
144+
// Claude models (Anthropic)
145+
case "claude-3-5-sonnet-20241022":
146+
case "claude-3-5-sonnet-20240620":
147+
case "claude-3-opus-20240229":
148+
case "claude-3-sonnet-20240229":
149+
case "claude-3-haiku-20240307":
150+
case "claude-2.1":
151+
return 200000;
152+
case "claude-2.0":
153+
case "claude-instant-1.2":
154+
return 100000;
155+
156+
// Gemini models (Google)
157+
case "gemini-1.5-pro":
158+
case "gemini-1.5-pro-latest":
159+
case "gemini-1.5-flash":
160+
case "gemini-1.5-flash-latest":
161+
return 1000000; // 1M tokens
162+
case "gemini-pro":
163+
case "gemini-pro-vision":
164+
return 32768;
165+
87166
default:
88167
return 4097;
89168
}

libs/langchain-core/src/language_models/tests/count_tokens.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ describe("getModelContextSize", () => {
3535
expect(await getModelContextSize("gpt-3.5-turbo")).toBe(4096);
3636
expect(await getModelContextSize("gpt-4")).toBe(8192);
3737
expect(await getModelContextSize("gpt-4-32k")).toBe(32768);
38+
expect(await getModelContextSize("gpt-5")).toBe(400000);
39+
expect(await getModelContextSize("gpt-5-turbo")).toBe(400000);
40+
expect(await getModelContextSize("gpt-5-turbo-preview")).toBe(400000);
41+
expect(await getModelContextSize("gpt-4o")).toBe(128000);
42+
expect(await getModelContextSize("gpt-4o-mini")).toBe(128000);
43+
expect(await getModelContextSize("gpt-4o-2024-05-13")).toBe(128000);
44+
expect(await getModelContextSize("gpt-4o-2024-08-06")).toBe(128000);
45+
expect(await getModelContextSize("gpt-4-turbo")).toBe(8192);
46+
expect(await getModelContextSize("gpt-4-turbo-preview")).toBe(8192);
47+
expect(await getModelContextSize("gpt-4-turbo-2024-04-09")).toBe(8192);
48+
expect(await getModelContextSize("gpt-4-0125-preview")).toBe(8192);
49+
expect(await getModelContextSize("gpt-4-1106-preview")).toBe(8192);
3850
});
3951
});
4052

0 commit comments

Comments
 (0)