Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
84 changes: 84 additions & 0 deletions packages/js/src/__tests__/calcPrice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,90 @@ describe('Core Price Calculation Function', () => {
})
})

it('should handle web search requests as total cost', () => {
const usage: Usage = {
input_tokens: 1000,
output_tokens: 500,
web_search_requests: 3,
}
const modelPrice: ModelPrice = {
input_mtok: 3.0,
output_mtok: 15.0,
web_search_kcount: 10, // $10 per 1000 web searches
}

const result = calcPrice(usage, modelPrice)

expect(result).toMatchObject({
input_price: 0.003, // 1000 * 3.0 / 1_000_000
output_price: 0.0075, // 500 * 15.0 / 1_000_000
total_price: 0.003 + 0.0075 + (10 * 3) / 1000, // add web search cost to total only
})
})

it('should handle file search requests as total cost', () => {
const usage: Usage = {
file_search_requests: 4,
input_tokens: 1000,
output_tokens: 500,
}
const modelPrice: ModelPrice = {
file_search_kcount: 2.5, // $2.50 per 1000 file searches
input_mtok: 2.5,
output_mtok: 10.0,
}

const result = calcPrice(usage, modelPrice)

expect(result).toMatchObject({
input_price: 0.0025, // 1000 * 2.5 / 1_000_000
output_price: 0.005, // 500 * 10.0 / 1_000_000
total_price: 0.0025 + 0.005 + (2.5 * 4) / 1000, // add file search cost to total only
})
})

it('should not add file search cost when requests is zero', () => {
const usage: Usage = {
file_search_requests: 0,
input_tokens: 1000,
output_tokens: 500,
}
const modelPrice: ModelPrice = {
file_search_kcount: 2.5,
input_mtok: 2.5,
output_mtok: 10.0,
}

const result = calcPrice(usage, modelPrice)

expect(result).toMatchObject({
input_price: 0.0025,
output_price: 0.005,
total_price: 0.0025 + 0.005,
})
})

it('should not add web search cost when requests is zero', () => {
const usage: Usage = {
input_tokens: 1000,
output_tokens: 500,
web_search_requests: 0,
}
const modelPrice: ModelPrice = {
input_mtok: 3.0,
output_mtok: 15.0,
web_search_kcount: 10,
}

const result = calcPrice(usage, modelPrice)

expect(result).toMatchObject({
input_price: 0.003,
output_price: 0.0075,
total_price: 0.003 + 0.0075,
})
})

it.each([
{
expected: { input_price: 0, output_price: 0, total_price: 0 },
Expand Down
41 changes: 41 additions & 0 deletions packages/js/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export const data: Provider[] = [
dest: 'output_tokens',
required: true,
},
{
path: ['server_tool_use', 'web_search_requests'],
dest: 'web_search_requests',
required: false,
},
],
},
{
Expand Down Expand Up @@ -115,6 +120,7 @@ export const data: Provider[] = [
cache_write_mtok: 1,
cache_read_mtok: 0.08,
output_mtok: 4,
web_search_kcount: 10,
},
},
{
Expand All @@ -138,6 +144,7 @@ export const data: Provider[] = [
cache_write_mtok: 3.75,
cache_read_mtok: 0.3,
output_mtok: 15,
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -167,6 +174,7 @@ export const data: Provider[] = [
cache_write_mtok: 3.75,
cache_read_mtok: 0.3,
output_mtok: 15,
web_search_kcount: 10,
},
},
{
Expand All @@ -182,6 +190,7 @@ export const data: Provider[] = [
cache_write_mtok: 0.3,
cache_read_mtok: 0.03,
output_mtok: 1.25,
web_search_kcount: 10,
},
},
{
Expand All @@ -198,6 +207,7 @@ export const data: Provider[] = [
cache_write_mtok: 18.75,
cache_read_mtok: 1.5,
output_mtok: 75,
web_search_kcount: 10,
},
},
{
Expand All @@ -214,6 +224,7 @@ export const data: Provider[] = [
cache_write_mtok: 3.75,
cache_read_mtok: 0.3,
output_mtok: 15,
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -242,6 +253,7 @@ export const data: Provider[] = [
cache_write_mtok: 1.25,
cache_read_mtok: 0.1,
output_mtok: 5,
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -270,6 +282,7 @@ export const data: Provider[] = [
cache_write_mtok: 18.75,
cache_read_mtok: 1.5,
output_mtok: 75,
web_search_kcount: 10,
},
},
{
Expand All @@ -292,6 +305,7 @@ export const data: Provider[] = [
cache_write_mtok: 18.75,
cache_read_mtok: 1.5,
output_mtok: 75,
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -320,6 +334,7 @@ export const data: Provider[] = [
cache_write_mtok: 6.25,
cache_read_mtok: 0.5,
output_mtok: 25,
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -380,6 +395,7 @@ export const data: Provider[] = [
},
],
},
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -411,6 +427,7 @@ export const data: Provider[] = [
cache_write_mtok: 3.75,
cache_read_mtok: 0.3,
output_mtok: 15,
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -465,6 +482,7 @@ export const data: Provider[] = [
},
],
},
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -519,6 +537,7 @@ export const data: Provider[] = [
},
],
},
web_search_kcount: 10,
},
},
{
Expand Down Expand Up @@ -8935,6 +8954,8 @@ export const data: Provider[] = [
input_mtok: 2,
cache_read_mtok: 0.5,
output_mtok: 8,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand All @@ -8957,6 +8978,8 @@ export const data: Provider[] = [
input_mtok: 0.4,
cache_read_mtok: 0.1,
output_mtok: 1.6,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand All @@ -8979,6 +9002,8 @@ export const data: Provider[] = [
input_mtok: 0.1,
cache_read_mtok: 0.025,
output_mtok: 0.4,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand Down Expand Up @@ -9021,6 +9046,8 @@ export const data: Provider[] = [
input_mtok: 2.5,
cache_read_mtok: 1.25,
output_mtok: 10,
web_search_kcount: 25,
file_search_kcount: 2.5,
},
},
{
Expand Down Expand Up @@ -9062,6 +9089,8 @@ export const data: Provider[] = [
input_mtok: 0.15,
cache_read_mtok: 0.075,
output_mtok: 0.6,
web_search_kcount: 25,
file_search_kcount: 2.5,
},
},
{
Expand Down Expand Up @@ -9215,6 +9244,8 @@ export const data: Provider[] = [
input_mtok: 1.25,
cache_read_mtok: 0.125,
output_mtok: 10,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand Down Expand Up @@ -9260,6 +9291,8 @@ export const data: Provider[] = [
input_mtok: 0.25,
cache_read_mtok: 0.025,
output_mtok: 2,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand All @@ -9281,6 +9314,8 @@ export const data: Provider[] = [
input_mtok: 0.05,
cache_read_mtok: 0.005,
output_mtok: 0.4,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand Down Expand Up @@ -9350,6 +9385,8 @@ export const data: Provider[] = [
input_mtok: 1.25,
cache_read_mtok: 0.125,
output_mtok: 10,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand All @@ -9376,6 +9413,8 @@ export const data: Provider[] = [
input_mtok: 0.25,
cache_read_mtok: 0.025,
output_mtok: 2,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand Down Expand Up @@ -9421,6 +9460,8 @@ export const data: Provider[] = [
input_mtok: 1.75,
cache_read_mtok: 0.175,
output_mtok: 14,
web_search_kcount: 30,
file_search_kcount: 2.5,
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions packages/js/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export function calcPrice(usage: Usage, modelPrice: ModelPrice): ModelPriceCalcu
if (modelPrice.requests_kcount !== undefined) {
totalPrice += modelPrice.requests_kcount / 1000
}
if (modelPrice.web_search_kcount && usage.web_search_requests) {
totalPrice += (modelPrice.web_search_kcount * usage.web_search_requests) / 1000
}
if (modelPrice.file_search_kcount && usage.file_search_requests) {
totalPrice += (modelPrice.file_search_kcount * usage.file_search_requests) / 1000
}

return {
input_price: inputPrice,
Expand Down
6 changes: 6 additions & 0 deletions packages/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ export interface Usage {
cache_audio_read_tokens?: number
cache_read_tokens?: number
cache_write_tokens?: number
file_search_requests?: number
input_audio_tokens?: number
input_tokens?: number
output_audio_tokens?: number
output_tokens?: number
web_search_requests?: number
}

export interface Tier {
Expand All @@ -28,11 +30,13 @@ export interface ModelPrice {
cache_audio_read_mtok?: number | TieredPrices
cache_read_mtok?: number | TieredPrices
cache_write_mtok?: number | TieredPrices
file_search_kcount?: number
input_audio_mtok?: number | TieredPrices
input_mtok?: number | TieredPrices
output_audio_mtok?: number | TieredPrices
output_mtok?: number | TieredPrices
requests_kcount?: number
web_search_kcount?: number
}

export interface ConditionalPrice {
Expand Down Expand Up @@ -73,10 +77,12 @@ export interface UsageExtractorMapping {
| 'cache_audio_read_tokens'
| 'cache_read_tokens'
| 'cache_write_tokens'
| 'file_search_requests'
| 'input_audio_tokens'
| 'input_tokens'
| 'output_audio_tokens'
| 'output_tokens'
| 'web_search_requests'
path: ExtractPath
required: boolean
}
Expand Down
Loading