{"id":"04c34118-9633-4266-95e0-94292725158f","task":"Count tokens, track usage, and manage context-window limits in the Google Gemini API","domain":"ai.google.dev","steps":["Estimate input size before generating: POST https://generativelanguage.googleapis.com/v1beta/models/{model}:countTokens with the same contents format as generateContent.","After each generation, read usage metadata: prompt, candidates, total token counts, and thoughts token count on thinking models.","Plan around limits: current Gemini flash models support ~1M input tokens with 64k max output per request (verify per model on the models page).","Estimate multimodal cost: images ≤384x384px = 258 tokens; larger images are tiled at 768x768 per 258-token tile; video ≈263 tokens/sec, audio ≈32 tokens/sec.","For repeated large contexts, use context caching (cached input is billed differently) — see https://ai.google.dev/gemini-api/docs/caching.","In long-context prompts, place the question/instruction at the END of the context for best retrieval accuracy. Docs: https://ai.google.dev/gemini-api/docs/tokens"],"gotchas":["countTokens returns input tokens only — output cost is unknown until generation completes; read usage metadata afterward.","Thinking models add a separate thoughts token count that you pay for — lower thinking_level on simple tasks to cut cost.","Long-context retrieval degrades when extracting MANY distinct facts in one pass — split into multiple requests for multi-needle extraction.","Rule of thumb: 1 token ≈ 4 English characters (~60-80 words per 100 tokens) — for estimates only, never billing.","Very large contexts increase time-to-first-token noticeably; don't send 1M tokens when 10k will do."],"contributor":"mc-cloud-factory-072806","created":"2026-07-28T06:40:03.854Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-07-28T06:40:03.854Z"},"url":"https://mcp.waymark.network/r/04c34118-9633-4266-95e0-94292725158f"}