Skip to content

Commit 8475c2c

Browse files
authored
Add GPT-5 to OpenAI cost callback (#284)
1 parent 9086026 commit 8475c2c

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

libs/community/langchain_community/callbacks/openai_info.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@
1010
from langchain_core.outputs import ChatGeneration, LLMResult
1111

1212
MODEL_COST_PER_1K_TOKENS = {
13+
# GPT-5 input
14+
"gpt-5": 0.00125,
15+
"gpt-5-cached": 0.000125,
16+
"gpt-5-2025-08-07": 0.00125,
17+
"gpt-5-2025-08-07-cached": 0.000125,
18+
# GPT-5 output
19+
"gpt-5-completion": 0.01,
20+
"gpt-5-2025-08-07-completion": 0.01,
21+
# GPT-5-mini input
22+
"gpt-5-mini": 0.00025,
23+
"gpt-5-mini-cached": 0.000025,
24+
"gpt-5-mini-2025-08-07": 0.00025,
25+
"gpt-5-mini-2025-08-07-cached": 0.000025,
26+
# GPT-5-mini output
27+
"gpt-5-mini-completion": 0.002,
28+
"gpt-5-mini-2025-08-07-completion": 0.002,
29+
# GPT-5-nano input
30+
"gpt-5-nano": 0.00005,
31+
"gpt-5-nano-cached": 0.000005,
32+
"gpt-5-nano-2025-08-07": 0.00005,
33+
"gpt-5-nano-2025-08-07-cached": 0.000005,
34+
# GPT-5-nano output
35+
"gpt-5-nano-completion": 0.0004,
36+
"gpt-5-nano-2025-08-07-completion": 0.0004,
37+
# GPT-5-chat-latest input
38+
"gpt-5-chat-latest": 0.00125,
39+
"gpt-5-chat-latest-cached": 0.000125,
40+
"gpt-5-chat-latest-2025-08-07": 0.00125,
41+
"gpt-5-chat-latest-2025-08-07-cached": 0.000125,
42+
# GPT-5-chat-latest output
43+
"gpt-5-chat-latest-completion": 0.01,
44+
"gpt-5-chat-latest-2025-08-07-completion": 0.01,
1345
# GPT-4.1 input
1446
"gpt-4.1": 0.002,
1547
"gpt-4.1-2025-04-14": 0.002,
@@ -319,7 +351,8 @@ def standardize_model_name(
319351
if "ft:" in model_name:
320352
model_name = model_name.split(":")[1] + "-finetuned"
321353
if token_type == TokenType.COMPLETION and (
322-
model_name.startswith("gpt-4")
354+
model_name.startswith("gpt-5")
355+
or model_name.startswith("gpt-4")
323356
or model_name.startswith("gpt-3.5")
324357
or model_name.startswith("gpt-35")
325358
or model_name.startswith("o1-")
@@ -331,7 +364,8 @@ def standardize_model_name(
331364
if (
332365
token_type == TokenType.PROMPT_CACHED
333366
and (
334-
model_name.startswith("gpt-4o")
367+
model_name.startswith("gpt-5")
368+
or model_name.startswith("gpt-4o")
335369
or model_name.startswith("gpt-4.1")
336370
or model_name.startswith("o1")
337371
or model_name.startswith("o3")

libs/community/tests/unit_tests/callbacks/test_openai_info.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from unittest.mock import MagicMock
23
from uuid import uuid4
34

@@ -145,6 +146,10 @@ def test_on_llm_end_finetuned_model(
145146
("gpt-4-32k", 0.18),
146147
("gpt-4-32k-0314", 0.18),
147148
("gpt-4-32k-0613", 0.18),
149+
("gpt-5", 0.01125),
150+
("gpt-5-mini", 0.00225),
151+
("gpt-5-nano", 0.00045),
152+
("gpt-5-chat-latest", 0.01125),
148153
],
149154
)
150155
def test_on_llm_end_azure_openai(
@@ -162,7 +167,7 @@ def test_on_llm_end_azure_openai(
162167
},
163168
)
164169
handler.on_llm_end(response)
165-
assert handler.total_cost == expected_cost
170+
assert math.isclose(handler.total_cost, expected_cost)
166171

167172

168173
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)