-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat: add token usage tracking to OpenAI adapter #7900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sestinj
wants to merge
2
commits into
main
Choose a base branch
from
nate/con-3935
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+365
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,353 @@ | ||
import { describe, expect, test, vi } from "vitest"; | ||
import { AnthropicApi } from "../apis/Anthropic.js"; | ||
import { GeminiApi } from "../apis/Gemini.js"; | ||
import { OpenAIApi } from "../apis/OpenAI.js"; | ||
import { CompletionUsage } from "openai/resources/index.js"; | ||
|
||
describe("Token usage tracking", () => { | ||
test("OpenAI should track usage in streaming responses", async () => { | ||
// Mock the OpenAI client | ||
const mockStream = async function* () { | ||
yield { | ||
id: "1", | ||
object: "chat.completion.chunk", | ||
created: Date.now(), | ||
model: "gpt-4", | ||
choices: [ | ||
{ | ||
index: 0, | ||
delta: { content: "Hello", role: "assistant" }, | ||
finish_reason: null, | ||
logprobs: null, | ||
}, | ||
], | ||
}; | ||
yield { | ||
id: "1", | ||
object: "chat.completion.chunk", | ||
created: Date.now(), | ||
model: "gpt-4", | ||
choices: [ | ||
{ | ||
index: 0, | ||
delta: { content: " world", role: "assistant" }, | ||
finish_reason: "stop", | ||
logprobs: null, | ||
}, | ||
], | ||
}; | ||
// Usage chunk | ||
yield { | ||
id: "1", | ||
object: "chat.completion.chunk", | ||
created: Date.now(), | ||
model: "gpt-4", | ||
choices: [], | ||
usage: { | ||
prompt_tokens: 10, | ||
completion_tokens: 5, | ||
total_tokens: 15, | ||
}, | ||
}; | ||
}; | ||
|
||
const api = new OpenAIApi({ apiKey: "test", provider: "openai" }); | ||
api.openai.chat.completions.create = vi.fn().mockResolvedValue(mockStream()); | ||
|
||
const stream = api.chatCompletionStream( | ||
{ | ||
model: "gpt-4", | ||
messages: [{ role: "user", content: "Hello" }], | ||
stream: true, | ||
}, | ||
new AbortController().signal | ||
); | ||
|
||
let content = ""; | ||
let usage: CompletionUsage | undefined; | ||
for await (const chunk of stream) { | ||
if (chunk.choices.length > 0) { | ||
content += chunk.choices[0].delta.content ?? ""; | ||
} | ||
if (chunk.usage) { | ||
usage = chunk.usage; | ||
} | ||
} | ||
|
||
expect(content).toBe("Hello world"); | ||
expect(usage).toBeDefined(); | ||
expect(usage?.prompt_tokens).toBe(10); | ||
expect(usage?.completion_tokens).toBe(5); | ||
expect(usage?.total_tokens).toBe(15); | ||
}); | ||
|
||
test("Anthropic should track usage in streaming responses", async () => { | ||
// Create a mock response that simulates Anthropic's SSE stream | ||
const mockResponseText = `event: message_start | ||
data: {"type":"message_start","message":{"usage":{"input_tokens":10,"cache_read_input_tokens":2}}} | ||
|
||
event: content_block_delta | ||
data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"Hello"}} | ||
|
||
event: content_block_delta | ||
data: {"type":"content_block_delta","delta":{"type":"text_delta","text":" world"}} | ||
|
||
event: message_delta | ||
data: {"type":"message_delta","usage":{"output_tokens":5}} | ||
|
||
event: message_stop | ||
data: {"type":"message_stop"} | ||
`; | ||
|
||
const mockResponse = { | ||
ok: true, | ||
status: 200, | ||
headers: new Headers({ "content-type": "text/event-stream" }), | ||
text: vi.fn().mockResolvedValue(mockResponseText), | ||
body: new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(new TextEncoder().encode(mockResponseText)); | ||
controller.close(); | ||
}, | ||
}), | ||
}; | ||
|
||
global.fetch = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
const api = new AnthropicApi({ apiKey: "test", provider: "anthropic" }); | ||
|
||
const stream = api.chatCompletionStream( | ||
{ | ||
model: "claude-3", | ||
messages: [{ role: "user", content: "Hello" }], | ||
stream: true, | ||
}, | ||
new AbortController().signal | ||
); | ||
|
||
let content = ""; | ||
let usage: CompletionUsage | undefined; | ||
for await (const chunk of stream) { | ||
if (chunk.choices.length > 0) { | ||
content += chunk.choices[0].delta.content ?? ""; | ||
} | ||
if (chunk.usage) { | ||
usage = chunk.usage; | ||
} | ||
} | ||
|
||
expect(content).toBe("Hello world"); | ||
expect(usage).toBeDefined(); | ||
expect(usage?.prompt_tokens).toBe(10); | ||
expect(usage?.completion_tokens).toBe(5); | ||
expect(usage?.total_tokens).toBe(15); | ||
expect(usage?.prompt_tokens_details?.cached_tokens).toBe(2); | ||
}); | ||
|
||
test("Gemini should track usage in streaming responses", async () => { | ||
// Create a mock response for Gemini streaming | ||
const mockResponseData = [ | ||
{ | ||
candidates: [ | ||
{ | ||
content: { | ||
parts: [{ text: "Hello" }], | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
candidates: [ | ||
{ | ||
content: { | ||
parts: [{ text: " world" }], | ||
}, | ||
}, | ||
], | ||
usageMetadata: { | ||
promptTokenCount: 10, | ||
candidatesTokenCount: 5, | ||
totalTokenCount: 15, | ||
}, | ||
}, | ||
]; | ||
|
||
const mockResponse = { | ||
ok: true, | ||
status: 200, | ||
headers: new Headers({ "content-type": "application/json" }), | ||
body: new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue( | ||
new TextEncoder().encode(JSON.stringify(mockResponseData)) | ||
); | ||
controller.close(); | ||
}, | ||
}), | ||
}; | ||
|
||
global.fetch = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
const api = new GeminiApi({ apiKey: "test", provider: "gemini" }); | ||
|
||
const stream = api.chatCompletionStream( | ||
{ | ||
model: "gemini-1.5-flash", | ||
messages: [{ role: "user", content: "Hello" }], | ||
stream: true, | ||
}, | ||
new AbortController().signal | ||
); | ||
|
||
let content = ""; | ||
let usage: CompletionUsage | undefined; | ||
for await (const chunk of stream) { | ||
if (chunk.choices.length > 0) { | ||
content += chunk.choices[0].delta.content ?? ""; | ||
} | ||
if (chunk.usage) { | ||
usage = chunk.usage; | ||
} | ||
} | ||
|
||
expect(content).toBe("Hello world"); | ||
expect(usage).toBeDefined(); | ||
expect(usage?.prompt_tokens).toBe(10); | ||
expect(usage?.completion_tokens).toBe(5); | ||
expect(usage?.total_tokens).toBe(15); | ||
}); | ||
|
||
test("OpenAI should pass through usage in non-streaming responses", async () => { | ||
const api = new OpenAIApi({ apiKey: "test", provider: "openai" }); | ||
|
||
const mockResponse = { | ||
id: "1", | ||
object: "chat.completion", | ||
created: Date.now(), | ||
model: "gpt-4", | ||
choices: [ | ||
{ | ||
index: 0, | ||
message: { | ||
role: "assistant", | ||
content: "Hello world", | ||
refusal: null, | ||
}, | ||
finish_reason: "stop", | ||
logprobs: null, | ||
}, | ||
], | ||
usage: { | ||
prompt_tokens: 10, | ||
completion_tokens: 5, | ||
total_tokens: 15, | ||
}, | ||
}; | ||
|
||
api.openai.chat.completions.create = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
const response = await api.chatCompletionNonStream( | ||
{ | ||
model: "gpt-4", | ||
messages: [{ role: "user", content: "Hello" }], | ||
stream: false, | ||
}, | ||
new AbortController().signal | ||
); | ||
|
||
expect(response.choices[0].message.content).toBe("Hello world"); | ||
expect(response.usage).toBeDefined(); | ||
expect(response.usage?.prompt_tokens).toBe(10); | ||
expect(response.usage?.completion_tokens).toBe(5); | ||
expect(response.usage?.total_tokens).toBe(15); | ||
}); | ||
|
||
test("Anthropic should track usage in non-streaming responses", async () => { | ||
const mockResponse = { | ||
ok: true, | ||
status: 200, | ||
json: vi.fn().mockResolvedValue({ | ||
id: "msg_123", | ||
content: [{ text: "Hello world" }], | ||
usage: { | ||
input_tokens: 10, | ||
output_tokens: 5, | ||
cache_read_input_tokens: 2, | ||
}, | ||
}), | ||
}; | ||
|
||
global.fetch = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
const api = new AnthropicApi({ apiKey: "test", provider: "anthropic" }); | ||
|
||
const response = await api.chatCompletionNonStream( | ||
{ | ||
model: "claude-3", | ||
messages: [{ role: "user", content: "Hello" }], | ||
stream: false, | ||
}, | ||
new AbortController().signal | ||
); | ||
|
||
expect(response.choices[0].message.content).toBe("Hello world"); | ||
expect(response.usage).toBeDefined(); | ||
expect(response.usage?.prompt_tokens).toBe(10); | ||
expect(response.usage?.completion_tokens).toBe(5); | ||
expect(response.usage?.total_tokens).toBe(15); | ||
expect(response.usage?.prompt_tokens_details?.cached_tokens).toBe(2); | ||
}); | ||
|
||
test("Gemini should track usage in non-streaming responses", async () => { | ||
// Gemini non-streaming uses the streaming method internally | ||
const mockResponseData = [ | ||
{ | ||
candidates: [ | ||
{ | ||
content: { | ||
parts: [{ text: "Hello world" }], | ||
}, | ||
}, | ||
], | ||
usageMetadata: { | ||
promptTokenCount: 10, | ||
candidatesTokenCount: 5, | ||
totalTokenCount: 15, | ||
}, | ||
}, | ||
]; | ||
|
||
const mockResponse = { | ||
ok: true, | ||
status: 200, | ||
headers: new Headers({ "content-type": "application/json" }), | ||
body: new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue( | ||
new TextEncoder().encode(JSON.stringify(mockResponseData)) | ||
); | ||
controller.close(); | ||
}, | ||
}), | ||
}; | ||
|
||
global.fetch = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
const api = new GeminiApi({ apiKey: "test", provider: "gemini" }); | ||
|
||
const response = await api.chatCompletionNonStream( | ||
{ | ||
model: "gemini-1.5-flash", | ||
messages: [{ role: "user", content: "Hello" }], | ||
stream: false, | ||
}, | ||
new AbortController().signal | ||
); | ||
|
||
expect(response.choices[0].message.content).toBe("Hello world"); | ||
expect(response.usage).toBeDefined(); | ||
expect(response.usage?.prompt_tokens).toBe(10); | ||
expect(response.usage?.completion_tokens).toBe(5); | ||
expect(response.usage?.total_tokens).toBe(15); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overwriting
global.fetch
without restoring leaves the mock active for subsequent tests. Please store the original fetch and restore it in afterEach/afterAll (or usevi.spyOn
) so other suites keep the real implementation.Prompt for AI agents