Skip to content

expose costs in ai chat response#2921

Merged
jelveh merged 2 commits intoHeyPuter:mainfrom
P3il4:expose-costs
May 6, 2026
Merged

expose costs in ai chat response#2921
jelveh merged 2 commits intoHeyPuter:mainfrom
P3il4:expose-costs

Conversation

@P3il4
Copy link
Copy Markdown
Contributor

@P3il4 P3il4 commented May 5, 2026

add usd_cents after a puter.ai.chat call:

// non-streaming
"usage": {
    "prompt_tokens": 12,
    "completion_tokens": 10,
    "cached_tokens": 0,
    "usd_cents": 0.002352
},
// streaming
{"type":"text","text":"hi."}
{"type":"usage","usage":{"prompt_tokens":11,"completion_tokens":9,"cached_tokens":0,"usd_cents":0.000705}}

usage is same as from puter.auth.getMonthlyUsage:

// from the non stream case
"deepseek:deepseek-chat:prompt_tokens": {
    "units": 12,
    "cost": 672,
    "count": 1
},
"deepseek:deepseek-chat:completion_tokens": {
    "units": 10,
    "cost": 1680,
    "count": 1
},
// 672 + 1680 = 2352 / 1_000_000 = 0.002352

ai.prompt.cost-calculated event shares the same logic for consistency
testing:

puter.ai.chat([{
        role: 'user',
        content: `message`,
        cache_control: { type: "ephemeral" } // mind the model minimal cache token length for testing cache cases
    },
], options={model: "claude-haiku-4-5"})

tested on openai (with, without cache), claude (with cache), openrouter (special "billedUsage" case) on live keys, deepseek/xai/gemini/mistral on mock servers
closes #2892

Copy link
Copy Markdown
Member

@Salazareo Salazareo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we call them microCents instead of Ucents

I know thats its meant to be the mu micro symbol, but bit less legible than microCents

@P3il4
Copy link
Copy Markdown
Contributor Author

P3il4 commented May 6, 2026

can we call them microCents instead of Ucents

I know thats its meant to be the mu micro symbol, but bit less legible than microCents

updated, the rest of the file + event uses ucents though, so this causes a bit of a naming inconsistency

@Salazareo
Copy link
Copy Markdown
Member

can we call them microCents instead of Ucents
I know thats its meant to be the mu micro symbol, but bit less legible than microCents

updated, the rest of the file + event uses ucents though, so this causes a bit of a naming inconsistency

well update as we touch it I guess, I didn't realize rest was also using ucents

@P3il4
Copy link
Copy Markdown
Contributor Author

P3il4 commented May 6, 2026

ignore that, it's back at efd7b19

@jelveh jelveh merged commit 5e9dc33 into HeyPuter:main May 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add usage.usd_cents to AI chat response usage object

4 participants