Image Token Calculator — Go source
Estimate the vision token cost of an image before sending it to an LLM - low/high/auto detail modes, the 512px tile math, the 2048/768 downscaling steps, and a full base + tiles + detail breakdown. Runs entirely in your browser.
This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Image Token Calculator — Go core (display excerpt).
//
// The authoritative Go source for this tool is the live CLI twin:
//
// cli/image-token-calculator/imagetokencalculator.go
//
// (package `imagetokencalculator`, the `cosmodev image-token-calculator`
// command foundation). It is kept in lock-step with the TypeScript lib
// src/lib/imageTokenCalculator.ts via shared table-driven test vectors, so
// the two implementations are held to one contract. This file carries only
// the core estimation signature that the twin exports:
package imagetokencalculator
// ImageTokens estimates the token cost of a width × height image at the
// given detail level, mirroring the TypeScript imageTokens(): `auto` stays
// low when both dimensions are ≤ 512 px; high detail downscales the image
// (fit inside 2048², then cap the shortest side at 768 px), tiles it into
// 512 px squares, and charges 170 tokens per tile on top of the fixed
// 85-token base view. Returns an error for non-positive dimensions or an
// unknown detail level.
func ImageTokens(width, height int, detail DetailLevel) (TokenBreakdown, error)
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →