Skip to content

AI 0.22.0: Daily spend caps, a request cache and structured JSON output

AI 0.22.0 lets a site cap what it spends on AI each day, answer repeated requests from a cache without paying again, and ask models for JSON that comes back already parsed.

Daily spend caps

Three settings cap spending over the last 24 hours: ai_daily_budget for every call, ai_daily_budget_per_endpoint for each endpoint and ai_daily_budget_per_user for each user a call names with user_uuid:. Amounts are in millionths of a dollar (1000000 is $1), and 0 means no cap. Once a cap is reached, every call is refused, cached answers included, with a message such as “The AI budget for the last 24 hours has been used up”. At 80 percent by default (ai_budget_warn_percent) a warning is logged and a telemetry event fires, once per crossing.

A request cache

Any call can pass cache: true (or a TTL, a key of its own, or :refresh) to reuse an earlier identical answer. A cached answer calls no provider and still writes a usage row, at zero cost and marked as cached. The cache is set up with config :phoenix_kit_ai, request_cache: [...]. Answers are shared between users, so give a personal answer a key of its own.

JSON and text extraction

ask/3, complete/3 and describe_image/3 accept schema: or json: true and return the parsed JSON. A model that refuses the JSON format setting gets one retry without it. The new extract_text/3 reads text from images with a vision model and returns typed blocks, the language, a legibility score and any fields you name.

Smaller changes

Host apps can add their own content to AI translation with config :phoenix_kit_ai, translatables: [...]. Every usage row records user_uuid:, and a telemetry event fires for each row. A paid vision call whose answer is prose instead of the requested JSON now keeps its cost in the usage log. A usage row that fails to insert logs a warning instead of vanishing.

Upgrading

No migration. The caps are off until you set them, and the cache is off unless a call asks for it or the config sets default: true.

Full release notes: AI 0.22.0

0.22.0 · 14 September 2026

Added

  • Spend caps (PR #26). PhoenixKitAI.Budget enforces daily caps over the trailing 24 hours, globally, per endpoint and per user_uuid:, set as nanodollar settings (ai_daily_budget, ai_daily_budget_per_endpoint, ai_daily_budget_per_user; 0 = no cap). Every provider-calling verb returns {:error, {:budget_exceeded, scope}} once a cap is reached, cached answers included. A [:phoenix_kit_ai, :budget, :warning] event and a log line fire once per crossing of ai_budget_warn_percent (default 80). Budget.status/2 reads spend without side effects.
  • Request cache. PhoenixKitAI.RequestCache (ETS, supervised by children/0): cache: true | [ttl:, key:, refresh:] | :refresh on every verb, configured through config :phoenix_kit_ai, request_cache: [...]. A hit calls no provider and writes a zero-cost usage row marked cached: true, keeping the attribution and prompt link.
  • Structured output. schema: / json: true on ask/3, complete/3 and describe_image/3 return the parsed JSON under "json" / :json. A model that rejects response_format gets one retry without it.
  • extract_text/3: OCR by vision model, with typed blocks, the language, a legibility score, has_illegible_text and caller-named fields:.
  • Host apps join the AI-translation pipeline via config :phoenix_kit_ai, translatables: [{type, module}].
  • A [:phoenix_kit_ai, :request] telemetry event fires per usage row.
  • user_uuid: is recorded on every verb’s usage row.
  • Saved-prompt rows carry metadata.prompt_snapshot.hash.
  • get_usage_stats/1 accepts until:.
  • Guide: dev_docs/guides/spend-caps-and-caching.md.

Fixed

  • dry_run: no longer lifts the spend cap on verbs that ignore it. Only process_image/4 honours dry_run:. Any other verb given the option skipped the cap and still called the provider.
  • A prose answer to a JSON vision request keeps its cost. It covers describe_image/3, extract_text/3 and compare_images/4. The paid call used to leave only a zero-cost error row, invisible to the spend caps. It now writes its normal success row, usage included, and the verb still returns {:error, {:no_json_in_response, text}}. Images.describe/3 takes an :on_no_json callback for this.
  • A caller cache key (cache: [key: …]) on extract_text/3 now includes fields:. A different field set no longer gets another set’s stored answer.
  • A usage row that fails to insert, such as one with an unknown user_uuid:, is logged as a warning instead of vanishing.
  • The reorder audit rows name the first id that exists, not the first one given.

AI 0.22.0. Install or update: {:phoenix_kit_ai, "~> 0.22"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.