Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@requesty/ai-sdk",
"version": "3.2.0",
"version": "3.3.0",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
Expand Down
45 changes: 45 additions & 0 deletions src/e2e/usage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { generateText, streamText } from 'ai'
import { describe, expect, it } from 'vitest'
import { createRequesty } from '..'
import { getTestModels } from './get-models'

const requesty = createRequesty({
apiKey: process.env.REQUESTY_API_KEY,
baseURL: process.env.REQUESTY_BASE_URL,
})

const modelsToTest = getTestModels()

describe.concurrent.each(modelsToTest)('Usage and Cost', ({ id }) => {
const model = requesty.chat(id)

describe.skip('generateText', () => {
it('should return cost in response', async () => {
const result = await generateText({
model,
prompt: 'Say hello in one word.',
maxOutputTokens: 100,
temperature: 0.3,
})

const raw = result.usage.raw
expect(raw).toBeDefined()
expect(raw!.cost).toBeTypeOf('number')
})
})

describe('streamText', () => {
it('should return cost in response', async () => {
const result = streamText({
model,
prompt: 'Say hello in one word.',
maxOutputTokens: 100,
temperature: 0.3,
})

const raw = (await result.usage).raw
expect(raw).toBeDefined()
expect(raw!.cost).toBeTypeOf('number')
})
})
})
9 changes: 9 additions & 0 deletions src/stream/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ describe('stream', () => {
cacheWrite: 5,
},
outputTokens: { total: 20, reasoning: 0, text: 20 },
raw: {
completion_tokens: 20,
prompt_tokens: 10,
prompt_tokens_details: {
caching_tokens: 5,
},
total_tokens: 30,
},
})
expect(requestyUsage.set).toHaveBeenCalledWith({
cachingTokens: 5,
Expand Down Expand Up @@ -495,6 +503,7 @@ describe('stream', () => {
cacheWrite: 0,
},
outputTokens: { total: 0, reasoning: 0, text: 0 },
raw: {},
})
expect(requestyUsage.set).toHaveBeenCalledWith({
cachingTokens: 0,
Expand Down
38 changes: 38 additions & 0 deletions src/usage/adapt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('getUsage', () => {
text: 50,
reasoning: 0,
},
raw: {
completion_tokens: 50,
prompt_tokens: 100,
prompt_tokens_details: {
cached_tokens: 20,
caching_tokens: 10,
},
total_tokens: 150,
},
})
})

Expand All @@ -53,6 +62,10 @@ describe('getUsage', () => {
text: 50,
reasoning: 0,
},
raw: {
completion_tokens: 50,
prompt_tokens: 100,
},
})
})

Expand All @@ -73,6 +86,7 @@ describe('getUsage', () => {
text: 0,
reasoning: 0,
},
raw: undefined,
})
})

Expand All @@ -93,6 +107,7 @@ describe('getUsage', () => {
text: 0,
reasoning: 0,
},
raw: {},
})
})

Expand All @@ -119,6 +134,13 @@ describe('getUsage', () => {
text: 50,
reasoning: 0,
},
raw: {
completion_tokens: 50,
prompt_tokens: 100,
prompt_tokens_details: {
cached_tokens: 20,
},
},
})
})

Expand Down Expand Up @@ -160,6 +182,14 @@ describe('getUsage', () => {
text: 0,
reasoning: 0,
},
raw: {
completion_tokens: 0,
prompt_tokens: 0,
prompt_tokens_details: {
cached_tokens: 0,
caching_tokens: 0,
},
},
})
})

Expand Down Expand Up @@ -187,6 +217,14 @@ describe('getUsage', () => {
text: 500000,
reasoning: 0,
},
raw: {
completion_tokens: 500000,
prompt_tokens: 1000000,
prompt_tokens_details: {
cached_tokens: 200000,
caching_tokens: 100000,
},
},
})
})
})
1 change: 1 addition & 0 deletions src/usage/adapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export const adaptUsage = (
// TODO adapt reasoning usage
reasoning: 0,
},
raw: requestyUsage,
}
}
1 change: 1 addition & 0 deletions src/usage/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export const RequestyUsageSchema = z
cached_tokens: z.number().optional(),
})
.optional(),
cost: z.number().positive().optional(),
})
.optional()
Loading