Packages
phoenix_kit_ai
0.31.0
0.31.0
0.30.0
0.29.1
0.29.0
0.28.0
0.27.0
0.26.1
0.26.0
0.25.5
0.25.4
0.25.3
0.25.2
0.25.1
0.25.0
0.24.1
0.24.0
0.23.2
0.23.1
0.23.0
0.22.0
0.21.0
0.20.1
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.2
0.18.1
0.18.0
0.17.1
0.17.0
0.16.0
0.15.1
0.15.0
0.14.1
0.14.0
0.13.0
0.12.2
0.12.1
0.12.0
0.11.2
0.11.1
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.1
0.2.0
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
AI module for PhoenixKit — endpoints, prompts, completions, and usage tracking
Current section
Files
Jump to
Current section
Files
phoenix_kit_ai
README.md
README.md
# PhoenixKitAI
AI module for PhoenixKit — endpoint management, prompt templates, chat
completions, embeddings, speech, image processing and generation, realtime
voice, and per-request usage tracking. Providers are OpenAI-compatible and
discovered at runtime from core's `PhoenixKit.Integrations` registry (every
provider with the `:ai_completions` capability — OpenRouter, OpenAI,
Anthropic, Mistral, DeepSeek, xAI, Z.ai, Amazon Bedrock, …), so there is no
hardcoded provider list here; Amazon Bedrock's non-OpenAI models go through
its Converse API. Implements the `PhoenixKit.Module` behaviour for
auto-discovery by a parent Phoenix application.
## Features
- **Endpoint Management** — Unified configuration combining provider
credentials, model selection, and generation parameters across three
built-in providers (OpenRouter, Mistral, DeepSeek)
- **Prompt Templates** — Reusable prompts with `{{Variable}}` substitution
syntax, live preview, and variable validation
- **Completions API** — Single-turn (`ask/3`), multi-turn (`complete/3`),
and embeddings (`embed/3`)
- **Speech** — text to speech (`speak/3`) and speech to text
(`transcribe/3`, through a chat model that hears or an
`/audio/transcriptions` model)
- **Reasoning capture** — Chain-of-thought from reasoning models
(DeepSeek-R1, Mistral Magistral, OpenAI o-series, Anthropic extended
thinking) automatically persisted to request metadata
- **Usage Tracking** — Every API call logged with tokens, cost
(US dollars), latency, status, and full caller context
- **Admin UI** — one `AI` tab with five subtabs: Endpoints, Prompts,
Playground, Usage (journal + retention), and Tasks (limits, guard, blocks)
- **Dynamic model selector** — Models auto-load from each provider's
`/models` endpoint when an integration is picked, with a 10-second
"still loading" hint and a retry button on transient failures
- **Real-time Updates** — PubSub broadcasts for endpoint/prompt/request
changes
- **Integrations-backed credentials** — API keys resolved via
`PhoenixKit.Integrations`, not stored per endpoint. Each endpoint
pins to a specific connection by uuid; the picker filters to the
current provider
## Quick start
Add to your parent app's `mix.exs`:
```elixir
{:phoenix_kit_ai, "~> 0.1"}
```
Run `mix deps.get` and start the server. The module appears in:
- **Admin sidebar** — AI with subtabs for Endpoints, Prompts, Playground,
Usage, and Tasks
- **Admin → Modules** — toggle on/off
- **Admin → Roles** — grant/revoke access per role
- **Admin → Settings → Integrations** — set up at least one provider
connection (OpenRouter is the only required one; any provider in core's
registry with the `:ai_completions` capability works). The endpoint
form's picker filters to whichever provider is selected on the dropdown.
## Installation
### Local development
```elixir
{:phoenix_kit_ai, path: "../phoenix_kit_ai"}
```
### Hex package
```elixir
{:phoenix_kit_ai, "~> 0.30"}
```
## API usage
### Simple chat completion
```elixir
{:ok, response} = PhoenixKitAI.ask(endpoint.uuid, "What is 2+2?")
{:ok, text} = PhoenixKitAI.extract_content(response)
# => "4"
```
### With system message
```elixir
{:ok, response} = PhoenixKitAI.ask(endpoint.uuid, "Hello",
system: "You are a pirate. Always respond like a pirate."
)
```
### Multi-turn conversation
```elixir
{:ok, response} = PhoenixKitAI.complete(endpoint.uuid, [
%{role: "system", content: "You are a helpful assistant."},
%{role: "user", content: "What's the weather like?"},
%{role: "assistant", content: "I don't have real-time weather data..."},
%{role: "user", content: "That's okay, just make something up."}
])
```
### Parameter overrides
```elixir
{:ok, response} = PhoenixKitAI.ask(endpoint.uuid, "Write a creative poem",
temperature: 1.5,
max_tokens: 500
)
```
### Embeddings
```elixir
# Single text
{:ok, response} = PhoenixKitAI.embed(endpoint.uuid, "Hello, world!")
# Batch
{:ok, response} = PhoenixKitAI.embed(endpoint.uuid, ["Text 1", "Text 2"])
# With dimension override
{:ok, response} = PhoenixKitAI.embed(endpoint.uuid, "Hello", dimensions: 512)
# Search: embed the query and the documents each the way the model expects
# (an `input_type`, a prefix or an instruction — `PhoenixKitAI.EmbeddingConventions`)
{:ok, q} = PhoenixKitAI.embed(endpoint.uuid, "horses on a beach", role: :query)
{:ok, d} = PhoenixKitAI.embed(endpoint.uuid, descriptions, role: :document)
# A long list, split by the model's per-request limits, merged in order
{:ok, all} = PhoenixKitAI.embed_many(endpoint.uuid, many_texts, role: :document)
# Pin OpenRouter's upstream provider so one index is not mixed
{:ok, d} =
PhoenixKitAI.embed(endpoint.uuid, texts,
role: :document,
provider: %{order: ["deepinfra"], allow_fallbacks: false},
normalize: true
)
# A non-English query against an English index (returns the query unchanged on any failure)
english = PhoenixKitAI.translate_query(chat_endpoint.uuid, "спящая кошка")
```
Per endpoint, `provider_settings["embedding"]` overrides the role
conventions (`"query_template"`, `"document_template"`, `"input_type"`,
`"instruction"`) and `provider_settings["provider_preferences"]` holds
OpenRouter provider preferences for every chat and embedding call (a
call's `provider:` is merged over it).
### Speech to text
```elixir
# `wav` is the recording's bytes (WAV, MP3, FLAC, OGG, WebM, M4A)
{:ok, %{text: text, language: language}} =
PhoenixKitAI.transcribe(endpoint.uuid, wav,
# an endpoint whose own model does not hear can name one that does
model: "google/gemini-2.5-flash",
instructions: "A customer ordering plywood. Write sizes as 2500×1250.",
language: "et"
)
# No speech in the recording is an answer, not an error
{:ok, %{text: ""}} = PhoenixKitAI.transcribe(endpoint.uuid, silence)
```
On OpenRouter the audio goes as an `input_audio` part of a chat request and
the model must take audio: the guard refuses a model that does not with
`{:error, {:model_unfit, [:audio_input]}}`. A speech-to-text model elsewhere
(`whisper-1`, `gpt-4o-transcribe`) gets `POST /audio/transcriptions`. The usage
row keeps the audio's format, size and length, never the audio.
### Dictation on a page
`PhoenixKitAI.Web.Components.Dictate` puts a recording button on a page and gives
the page the words: core's audio recorder, `transcribe/3` off the LiveView process,
the busy state and the error messages, with no upload or JavaScript of your own.
It needs a core with `PhoenixKitWeb.Components.Core.AudioRecorder`; on an older one
it shows nothing to record with.
```heex
<.live_component
module={PhoenixKitAI.Web.Components.Dictate}
id="inquiry-voice"
endpoint="Wood Matrix"
model="google/gemini-2.5-flash"
instructions={MyApp.Voice.instructions()}
label={gettext("Say it instead")}
into="inquiry-text"
user_uuid={@user_uuid}
/>
```
```elixir
def handle_info({:dictated, "inquiry-voice", text}, socket), do: ...
```
`text` is trimmed and never empty. `into` is optional: it adds the words to that
`<textarea>` or `<input>` (`mode={:append}`, `:replace` or `:at_cursor`) and fires
`input`, so the form's `phx-change` runs; the message comes either way. A LiveView
that defines `handle_info/2` needs a clause for it, or a catch-all. Any other
attribute goes to `transcribe/3`, and calls run as the `ai.dictation` task unless
`task` names your own, so limits, blocks and recommendations are on AI → Tasks.
A failure is shown under the button, translated. With `show_error={false}` the page
gets `{:dictation_failed, id, reason}` instead, and `Dictate.message/1` has the words.
### Structured output
```elixir
{:ok, %{"json" => %{"axes" => axes}}} =
PhoenixKitAI.ask(endpoint_uuid, "Propose 3–5 rating axes for chocolate bars.",
schema: %{"type" => "object", "properties" => %{"axes" => %{"type" => "array", "items" => %{"type" => "string"}}}})
{:ok, %{"json" => %{}}} = PhoenixKitAI.complete(endpoint_uuid, messages, json: true)
```
The schema goes to the provider as `response_format` and into the prompt, so
models without `response_format` still answer in shape (one retry without
it); the parsed object comes back under `"json"`. Nothing validates the
answer against the schema locally — check the keys you depend on.
### Spend caps and the request cache
```elixir
# Caps in US dollars, as a string or a Decimal; 0 = no cap. The daily caps look
# at the trailing 24 hours, the monthly one at the calendar month of the site
# time zone.
# An integer other than 0 is refused ({:error, :ambiguous_amount}): releases
# before this one took integer millionths of a dollar.
PhoenixKitAI.Budget.set_limit(:global, "5") # $5 per rolling day for the whole install
PhoenixKitAI.Budget.set_limit(:global_month, "100") # $100 per calendar month for the whole install
PhoenixKitAI.Budget.set_limit(:user, "0.10") # $0.10 per rolling day per user_uuid
# user_uuid must be a PhoenixKit user uuid (the usage row has a foreign key on it)
{:error, {:budget_exceeded, :user}} = PhoenixKitAI.ask(ep, "…", user_uuid: current_user.uuid)
# scopes: :global, :global_month, :endpoint, :user, :user_calls
PhoenixKitAI.Budget.status(endpoint_uuid, user_uuid: current_user.uuid) # spent / limit / remaining per scope
PhoenixKitAI.get_usage_stats(user_uuid: current_user.uuid, since: yesterday) # what they spent
# Cache an answer for a day; the second identical call costs nothing
{:ok, _} = PhoenixKitAI.ask(ep, prompt, cache: true)
{:ok, _} = PhoenixKitAI.ask(ep, prompt, cache: [ttl: 3_600])
{:ok, _} = PhoenixKitAI.ask(ep, prompt, cache: [key: {current_user.uuid, "profile"}])
{:ok, _} = PhoenixKitAI.ask(ep, prompt, cache: :refresh)
```
The daily caps cover the trailing 24 hours (not a calendar day); the
monthly one (`ai_monthly_cap`) the calendar month of the site time zone and
starts again on the first. Caps are checked before
every provider call — cached answers included, so a spent cap stops the
site — and warn once at 80 %. The cache is in-memory (ETS), keyed on what
would be sent (or on your own `cache: [key: …]`), shared across users
unless your key says otherwise, writes a zero-cost usage row on a hit, and
empties on restart — pair it with your own persistence for "write once,
keep forever". Every usage row emits `[:phoenix_kit_ai, :request]`
telemetry; the full contract is in
`dev_docs/guides/spend-caps-and-caching.md`.
### AI tasks and the model check
A module declares what it uses AI for, and what the model must be able to
do, in an `ai_tasks/0` callback; calls name the task.
```elixir
def ai_tasks do
[
%PhoenixKitAI.TaskSpec{
key: "my_module.photo_count",
label: "Count items in a photo",
purpose: "Count the stock items visible in a shelf photo.",
needs: [:vision, :json_schema],
endpoints: {MyModule.Settings, :photo_endpoint_uuids, []}
}
]
end
{:ok, %{json: json, request_uuid: row}} =
PhoenixKitAI.describe_image(endpoint_uuid, photos, task: "my_module.photo_count", schema: schema)
# A settings form, before saving the chosen endpoint
:ok = PhoenixKitAI.Tasks.check_selection("my_module.photo_count", endpoint_uuid)
PhoenixKitAI.Tasks.endpoint_options("my_module.photo_count") # [%{endpoint:, fitness:}]
```
Before any HTTP, every verb checks the model (or the call's `model:`
override) against the task's `needs` and what the call itself shows —
image parts, file parts, `tools:`, `schema:` / `json:`, and image output
for `generate_image` / `edit_image` / `process_image`. A model that says
no to images, documents, image generation, tools or a JSON schema is
refused with
`{:error, {:model_unfit, missing}}`, written as an error row marked
`refused: true` that no usage sink receives; an unknown capability passes
unless `ai_guard_verified_models_only` is on. With it on, providers whose
model list says nothing about capabilities (OpenAI, xAI, Z.ai) need them
marked on the endpoint — image generation included — or their calls are
refused. Each usage row carries
`metadata.task`, and each result the uuid of its row (`"request_uuid"` /
`:request_uuid`). «AI → Tasks» lists the declared tasks with their
endpoints and fitness.
### Tasks from your own app
An app that calls PhoenixKitAI directly, without being a PhoenixKit
module, declares its tasks in a module of its own and lists it in config
(read at run time):
```elixir
# config/config.exs
config :phoenix_kit_ai, task_providers: [MyApp.AI]
```
```elixir
defmodule MyApp.AI do
alias PhoenixKitAI.TaskSpec
def ai_tasks do
[
%TaskSpec{
key: "myapp.photo_fix",
label: "Read a product photo",
needs: [:vision, :json_schema],
default_limits: %{task: %{requests: %{day: 500}}}
}
]
end
end
PhoenixKitAI.describe_image(endpoint_uuid, photos, task: "myapp.photo_fix", schema: schema)
```
A provider's tasks work like a module's — limits and personal limits,
`user_limits: false`, watchers, the model check, `metadata.task` on every
usage row, «AI → Tasks» (marked as declared by your app) — and are always
enabled, since a provider has no module switch. Notes:
- **Namespace the keys** (`"myapp.photo_fix"`). A key a PhoenixKit module
already declares stays the module's: the provider's task is dropped, with
a warning naming both and a notice on «AI → Tasks». Between providers the
first in the list wins.
- `ai_tasks/0` is read once and cached until the list of modules or of
providers changes, so keep feature flags out of it.
- Registered PhoenixKit modules are found on their own — don't list them.
One listed anyway counts as a module (its switch applies).
- A listed module that is not loaded, has no `ai_tasks/0` or raises is
warned about and skipped; a raising one is tried again a minute later.
- A task of a module that is switched off refuses calls with
`{:error, {:task_disabled, key}}` (a refusal row; a cached answer does not
bypass it). With the AI module itself off, its own `ai.translation`,
`ai.playground` and `ai.dictation` are refused the same way. Calls without `task:`, or naming
a key nobody declares, are not affected.
### Task limits
A task can be limited — the whole task, and each user in it — in money,
requests and tokens, per calendar day and month of the site time zone. An
admin sets them on «AI → Tasks» (with the `ai.manage` permission), a
declaration can give defaults, and a user can have personal limits:
```elixir
%PhoenixKitAI.TaskSpec{
key: "my_module.photo_count",
default_limits: %{user: %{requests: %{day: 50}}, task: %{money: %{month: "20.00"}}},
watchers: {MyModule.Settings, :limit_watchers, []} # called with the user uuid appended
}
{:error, {:task_limit_exceeded, :user, :requests, :day}} =
PhoenixKitAI.ask(endpoint_uuid, "…", task: "my_module.photo_count", user_uuid: user_uuid)
# Several calls for one piece of work: the limits are checked once.
{:ok, token} = PhoenixKitAI.Guard.admit("my_module.photo_count", user_uuid, turn_ref, 300)
PhoenixKitAI.ask(endpoint_uuid, "…", task: "my_module.photo_count", user_uuid: user_uuid,
guard_admission: token)
```
Money is in US dollars (a `Decimal` or a dollar string — never an integer,
which releases before this one read as millionths of a dollar) and counts
where a call has a cost: the
provider's own (OpenRouter), else a catalogue price, the endpoint's manual
price (`provider_settings["pricing"]`, set by a holder of `ai.manage`) or
the built-in Anthropic list price — `PhoenixKitAI.reports_cost?/1` says
whether an endpoint has one, and the usage row's `metadata.cost_source`
which one counted. The vision verbs (`describe_image`, `extract_text`,
`compare_images`) are priced like chat, for the model the answer names, else
the call's `model:`, else the endpoint's; making or editing images is priced
per image or per call and counts only the provider's own cost
(`TaskLimits.money_counted?/2` says so for a task that needs `:image_output`;
one that needs `:vision` gets the chat answer). An empty limit is none, `0`
allows nothing. Only successful calls count — a cached answer is one
request — and a refusal is an error row like the model check's. At the
warn percent (default 80) and when a limit is reached, the user and the
watchers (`ai_guard_watchers`, else every Owner, plus the task's own) get a
notification of type `"ai_guard"`, once per window; a `Budget` refusal is
told once a day, and a task whose endpoint's model stops fitting once a day
per endpoint.
A module that limits each of its users itself declares the task with
`user_limits: false`: the guard then has only the task's limits for it,
ignores the user part of `default_limits` and refuses user limits
(`{:error, :user_limits_off}`); «AI → Tasks» shows no user limits for it.
### Off-purpose use and blocks
The consumer has its model flag requests that are not what the task is
for; the guard never edits a prompt or a schema. It gives the instruction
text and takes the report:
```elixir
if PhoenixKitAI.Guard.self_flag?("my_module.photo_count") do
# Into the system prompt, plus the consumer's own way to flag
# (a field of its JSON schema, a marker in the answer):
PhoenixKitAI.Guard.self_flag_instruction("my_module.photo_count")
end
# When the model flagged a call (once per call, or per turn with a turn_ref):
PhoenixKitAI.Guard.report_off_purpose("my_module.photo_count", user_uuid, reason, request_uuid)
PhoenixKitAI.Guard.report_off_purpose("my_module.chat", user_uuid, reason, %{turn_ref: turn_ref})
```
The watchers are told of a person's first flag in a task each day, never
with the reason. An admin with `ai.manage` turns self-flagging off (or on)
per task on «AI → Tasks»; `self_flag?/1` follows at once. A declaration may also set norms that need no content
(`sources:`, `max_calls_per_hour:`, `max_tokens_per_call:`); a broken one
is signalled once a day and blocks nothing.
A person with `ai.manage` (or an Owner) blocks someone from a task on
«AI → Tasks», from a flag or the person's row; the block lands on the
task's `block_group` (the Playground and free-text tasks share
`free_text`), and the person's calls are refused with
`{:error, {:task_blocked, group}}` until it is lifted. Only an Owner blocks
an Owner or lifts their block, and nobody but an Owner lifts their own. A
`private: true` task shows the reasons of its flags and blocks only to
`ai.manage` and Owners.
### Host apps in the translation pipeline
```elixir
config :phoenix_kit_ai, translatables: [{"product", MyApp.AI.ProductTranslatable}]
```
The module implements `PhoenixKitAI.Translatable` — `fetch/2`,
`source_fields/2`, `put_translation/4` — and its records are then queued
with `PhoenixKitAI.Translations.enqueue/1` like any kit module's. The shared
prompt binds the field names `name`, `title`, `summary`, `description`,
`body` and `content`; an adapter with other field names passes its own
`prompt_uuid`, or those values never reach the model. A configured entry
wins over a kit module's adapter for the same type.
### Image editing and processing
Image-in, image-out through any provider, with the endpoint choosing the
transport:
```elixir
# Named operations, fitted to what the model accepts:
{:ok, %{images: [%{data: png, content_type: "image/png"}], warnings: warnings}} =
PhoenixKitAI.process_image(endpoint_uuid, [photo_jpeg], [
:remove_reflections,
{:clean_background, color: "white"},
{:upscale, resolution: "2K"}
])
# Or a plain instruction with reference images:
{:ok, %{images: [%{data: png}]}} =
PhoenixKitAI.edit_image(endpoint_uuid, "Restyle the first photo like the second.",
[%{data: room_jpeg, content_type: "image/jpeg"}, %{data: reference_jpeg, content_type: "image/jpeg"}],
aspect_ratio: "4:3")
# Vision: free text, or a parsed JSON object per schema
{:ok, %{json: %{"brand" => brand}}} =
PhoenixKitAI.describe_image(endpoint_uuid, label_jpeg,
prompt: "Read the label.",
schema: %{"type" => "object", "properties" => %{"brand" => %{"type" => "string"}}})
# Text extraction (OCR through the vision model): full transcription,
# typed blocks, language, legibility, plus any named values you want pulled out
{:ok, %{text: text, fields: %{"ean" => ean, "best_before" => date}}} =
PhoenixKitAI.extract_text(endpoint_uuid, label_jpeg,
fields: %{"ean" => "the barcode digits", "best_before" => "expiry date as YYYY-MM-DD"})
```
Operations (`PhoenixKitAI.Images.Operations`): `:clean_background`,
`:blur_background`, `:remove_background`, `:replace_background`,
`:remove_reflections`, `:remove_objects`, `:enhance`, `:upscale`,
`:relight`, `:recolor`, `:straighten`, `:crop_to_subject`, `:restyle`,
plus free text — extendable from config, and any operation's wording can
be overridden by a saved prompt named `Image op <name>`.
Providers: OpenRouter's unified Images API (every image model on the
gateway, with per-model capabilities), xAI, OpenAI, and any
OpenAI-compatible API through chat completions. Adding one is a
`PhoenixKitAI.Provider` adapter plus a config entry. The module stores no
files: results come back as bytes for the caller to keep.
### Extracting response data
```elixir
{:ok, response} = PhoenixKitAI.ask(endpoint.uuid, "Hello!")
# Text content
{:ok, text} = PhoenixKitAI.extract_content(response)
# Usage statistics (cost in US dollars, a Decimal)
usage = PhoenixKitAI.extract_usage(response)
# => %{prompt_tokens: 10, completion_tokens: 15, total_tokens: 25, cost: Decimal.new("0.00003")}
# Full response includes latency
response["latency_ms"] # => 850
```
### Reasoning models — chain-of-thought capture
Reasoning models (DeepSeek-R1, Mistral Magistral, OpenAI o-series,
Anthropic extended thinking) return their chain-of-thought alongside
the final answer in a per-provider field on the assistant message.
PhoenixKitAI normalizes the three known shapes and persists the trace
into request metadata so operators can inspect it from the admin
Usage page (collapsed by default — chains-of-thought routinely run
5-50× the length of the answer).
```elixir
{:ok, response} = PhoenixKitAI.complete(endpoint.uuid, [
%{role: "user", content: "If a train leaves Chicago at 2pm..."}
])
# Final answer
{:ok, answer} = PhoenixKitAI.extract_content(response)
# Chain-of-thought (if the model produced one)
PhoenixKitAI.Completion.extract_reasoning(response)
# => "Step 1: identify the variables...\nStep 2: ..."
# => nil when the model isn't a reasoning model
```
Field-name normalization (handled internally; you don't need to know
which shape your provider uses):
| Provider / response shape | Field on `message` |
|---|---|
| OpenRouter and what it proxies | `reasoning` |
| DeepSeek native API | `reasoning_content` |
| Some others | `thinking` |
The trace lands in `phoenix_kit_ai_requests.metadata.response_reasoning`.
Subject to the same `capture_request_content?` privacy gate as
`response` content (see "Privacy / retention controls" below) — when
content capture is off (globally, or for the call with
`capture_request_content: false`), reasoning is dropped too. Reasoning can mirror
prompt content and is PII-equivalent.
## Prompt templates
Prompts are reusable templates with `{{VariableName}}` substitution.
Variable names must start with a letter or underscore.
```elixir
{:ok, prompt} = PhoenixKitAI.create_prompt(%{
name: "Email Writer",
content: "Write a professional email about {{Topic}} to {{Recipient}}."
})
{:ok, response} = PhoenixKitAI.ask_with_prompt(
endpoint.uuid,
"email-writer", # accepts uuid, slug, or struct
%{"Topic" => "Q4 results", "Recipient" => "stakeholders"}
)
```
Other helpers: `get_prompt_variables/1`, `preview_prompt/2`,
`validate_prompt_variables/2`, `search_prompts/2`,
`get_prompts_with_variable/1`, `list_prompts/0`, `list_enabled_prompts/0`,
`enable_prompt/1`, `disable_prompt/1`, `duplicate_prompt/2`,
`delete_prompt/1`, `get_prompt_usage_stats/0`, `reset_prompt_usage/1`.
## Endpoint management
```elixir
# Create — OpenRouter
{:ok, endpoint} = PhoenixKitAI.create_endpoint(%{
name: "Claude Fast",
provider: "openrouter", # provider key (see Supported providers)
integration_uuid: integration.uuid,
model: "anthropic/claude-3-haiku",
temperature: 0.7
})
# Create — Mistral. base_url defaults to https://api.mistral.ai/v1
# via Endpoint.default_base_url/1; model id is provider-native.
{:ok, ep} = PhoenixKitAI.create_endpoint(%{
name: "Mistral Large",
provider: "mistral",
integration_uuid: mistral_integration.uuid,
model: "mistral-large-latest"
})
# Create — DeepSeek
{:ok, ep} = PhoenixKitAI.create_endpoint(%{
name: "DeepSeek Reasoner",
provider: "deepseek",
integration_uuid: deepseek_integration.uuid,
model: "deepseek-reasoner" # reasoning model — chain-of-thought captured
})
# List with filters and sorting
PhoenixKitAI.list_endpoints(
provider: "openrouter",
enabled: true,
sort_by: :usage,
sort_dir: :desc
)
# Update / toggle / delete
{:ok, updated} = PhoenixKitAI.update_endpoint(endpoint, %{temperature: 0.5})
{:ok, _} = PhoenixKitAI.update_endpoint(endpoint, %{enabled: false})
{:ok, _} = PhoenixKitAI.delete_endpoint(endpoint)
```
> **API keys are managed via Integrations.** Each endpoint references a
> specific `PhoenixKit.Integrations` connection by uuid via the
> `integration_uuid` column (added in core's V107 with backfill from
> existing `provider` strings). The picker on the endpoint form writes
> the chosen connection's uuid; `OpenRouterClient.resolve_api_key/1`
> looks up credentials by uuid at request time — no per-provider
> guessing. After a successful migration (manual save with an
> integration picked, or `migrate_legacy/0` at boot), the legacy
> `api_key` column is atomically wiped to `""` so the credential lives
> in exactly one place. The column itself stays in the schema (it's
> `NOT NULL` in core's V34, so the value must be a string — empty
> string represents "cleared") so a manual DB recovery is still
> possible if catastrophe strikes; planned for removal in a future
> major version.
> See [Migrating from legacy `endpoint.api_key`](#migrating-from-legacy-endpointapi_key)
> for the recommended workflow and the boot-time auto-migrator.
## Source tracking & debugging
Every request automatically captures:
- **Source** — clean caller identifier (e.g.
`"MyApp.ContentGenerator.summarize"`), auto-extracted from the
stacktrace. Override via `source: "CustomLabel"`.
- **Stacktrace** — up to 20 frames.
- **Caller context** — `request_id` (Phoenix Logger metadata), `node`,
`pid`.
- **Message + response content** — full user message list and assistant
response text. Default-on; controllable via the config flag below.
- **Memory snapshot** (opt-in) — enable with
`config :phoenix_kit_ai, :capture_request_memory, true` when you need
per-request memory data; off by default to keep JSONB metadata small.
All of this is stored in `phoenix_kit_ai_requests.metadata` (JSONB) and
surfaced in the admin Usage page's request-details modal.
### Privacy / retention controls
# Image processing (see dev_docs/guides/image-processing.md)
provider_adapters: %{}, # provider key => PhoenixKitAI.Provider module
image_operations: %{}, # extra / replacement named operations
max_image_bytes: 25_000_000, # largest input or fetched output
allow_internal_image_urls: false # lift the image-fetch host policy (tests only)
```elixir
# config/config.exs (defaults shown)
config :phoenix_kit_ai,
# Persist user message + assistant response content in request
# metadata. Default `true` matches the shipped debugging shape.
# Set to `false` for deployments with PII / data-retention
# obligations — token counts, latency, model, and cost are still
# recorded; only the user-supplied strings get redacted (replaced
# with `metadata.content_redacted: true`). A call can only narrow this:
# `capture_request_content: false` on complete/3 or an image verb redacts
# that call; `true` on a call never overrides a global `false`
# (speak/3 and embed/3 ignore the per-call option; transcribe/3 honours it).
capture_request_content: true,
# Capture process memory in request `caller_context` (default off).
capture_request_memory: false,
# Bypass SSRF guard on `Endpoint.base_url` — required for
# self-hosted Ollama / intranet inference. Off by default; the
# guard rejects loopback / RFC1918 / link-local / `*.local` /
# non-http(s) URLs unless this is enabled.
allow_internal_endpoint_urls: false
```
#### Journal retention
By default nothing is deleted: the request journal, its content and the
guard's events are kept forever. On **AI → Usage** a person with
`ai.manage` (or an Owner) can set a period for each of three kinds — N
days, months or years, or "keep forever":
- **Content of journal entries** — older rows lose the message bodies,
responses, reasoning, tool calls, inputs and debug context from
`metadata` (`PhoenixKitAI.Retention.content_keys/0`); the row, its cost,
tokens and model stay, and the request's details say when the content
was removed.
- **Journal entries** — older rows of `phoenix_kit_ai_requests` are
deleted, cost included.
- **Guard events** — older rows of `phoenix_kit_ai_guard_events` are
deleted.
A period is applied by the nightly job `PhoenixKitAI.JournalPruneWorker`
(`oban_crons/0`, written into the host's crontab by
`mix phoenix_kit.update`), no earlier than a day after it was saved; a
shorter period is saved only after a preview of what it would delete. The
same card has "Clean up now" (count first, then confirm). Rows of the
current month — from local midnight two days before its first day — and
the content of the last 24 hours are never removed, whatever the period:
the spend caps and task limits read them. Each manual run, each night
that removed something and failed runs are in the Activity feed
(`journal.pruned` / `journal.prune_failed`, kept permanently).
Options of the run (chunk sizes, pauses, time slices) are
`config :phoenix_kit_ai, :journal_prune`. The full picture for a host,
including the first run on a large journal:
`dev_docs/guides/journal-retention.md`.
## Migrating from legacy `endpoint.api_key`
Endpoints created before V107 / the Integrations migration stored the
OpenRouter API key directly in the `api_key` column and used the bare
`provider` field (`"openrouter"`) without a specific connection
reference. V107's backfill stamps `integration_uuid` for any endpoint
whose `provider` matches a `PhoenixKit.Integrations` row. Endpoints
that can't be auto-resolved keep working via the legacy `api_key`
column with a deprecation warning per request — until they're
migrated, at which point the column is atomically wiped.
The recommended workflow is to point each endpoint at a specific
integration connection via the form's connection picker (a
provider's only connection with credentials is picked on its own). The
endpoint changeset clears the legacy column to `""` in the same
DB transaction (`Endpoint.maybe_clear_legacy_api_key/1`), so once
migrated the credential lives only in the integration row.
Stuck endpoints (`api_key` populated, `integration_uuid` still NULL —
e.g., when V107 couldn't match anything and the boot-time migrator
hasn't reached this endpoint) get a "Legacy API key (recovery)" card
on the edit form, with a copy button so the operator can paste the
key into a new Integration without bouncing back to OpenRouter. The
card disappears once an integration is selected and saved.
### Manual workflow (per-endpoint)
1. Open **Settings → Integrations** and add an OpenRouter connection if
one doesn't exist.
2. Edit the endpoint and select that connection in the connection
picker.
3. Save. The legacy warning stops firing on the next request.
### Boot-time auto-migrator
The recommended entry point is the orchestrator
`PhoenixKit.ModuleRegistry.run_all_legacy_migrations/0`. Call it once from
your host app's `Application.start/2`; it walks every registered
PhoenixKit module and invokes its `migrate_legacy/0` callback (idempotent
per module, never crashes the boot):
```elixir
def start(_type, _args) do
children = [...]
result = Supervisor.start_link(children, opts)
# One call. Walks all modules. Per-module errors caught + logged.
PhoenixKit.ModuleRegistry.run_all_legacy_migrations()
result
end
```
For AI specifically, `PhoenixKitAI.migrate_legacy/0` (the callback)
runs both kinds of legacy data migration: the api_key→Integrations
credentials migration AND the provider-string→integration_uuid reference
sweep. Both emit `PhoenixKit.Activity` entries (`action:
"integration.legacy_migrated"`) with PII-safe metadata so operators can
audit migrations from the activity feed.
Behaviour:
- Targets endpoints with `provider == "openrouter"` (the bare default —
named connections like `"openrouter:my-key"` are NEVER touched) AND a
non-empty `api_key`.
- Groups by `api_key` value (endpoints sharing a key share one
connection — dedup).
- Creates one Integrations connection per distinct key. Naming:
`"openrouter:default"` for single-key deployments;
`"openrouter:imported-1"` / `"imported-2"` / etc. for multi-key.
- Updates each endpoint's `provider` string AND `integration_uuid`
to point at the new connection — atomically, in a single
`Repo.update_all`.
- **Clears the legacy `api_key` column atomically with the linking**
(set to `""`). The credential lives in exactly one place after
migration; the runtime resolver takes the `integration_uuid` path,
and a broken integration surfaces a loud error rather than silently
coasting on a stale duplicate key.
- *Un*-migrated endpoints (skipped by the where-clause filter, or by
one of the idempotency gates) keep their `api_key` populated as a
safety-net fallback until they're explicitly handled — either via
the manual UI workflow or a re-run of the auto-migrator after the
blocking condition clears.
Idempotency guards (any one short-circuits): completion-flag setting,
existing `integration:openrouter:*` key in `phoenix_kit_settings`, no
endpoints needing migration. Failure modes are contained — top-level
`try/rescue/catch :exit` shell ensures the migration NEVER crashes the
host-app boot.
The migration is opt-in (you must call the function explicitly).
Operators who prefer the manual UI workflow can simply not call it —
the resolver's legacy fallback path keeps existing endpoints working
indefinitely.
The `api_key` column itself is flagged **Deprecated** in
`PhoenixKitAI.Endpoint` and is planned for removal in a future major
version.
### Legacy `raw_response` metadata
Older request rows stored the full raw API response under
`metadata["raw_response"]` for debugging. This was removed to avoid
persisting provider responses verbatim; new rows omit the key entirely.
The admin usage UI still renders the raw-response panel when it's
present in a row's metadata, so historic data stays viewable.
## Error handling
All public functions return `{:ok, result}` or `{:error, reason}` where
`reason` is an **atom** from a known set:
| Atom | Cause | Translated message (via `PhoenixKitAI.Errors.message/1`) |
|------|-------|-----|
| `:endpoint_not_found` | Endpoint UUID does not exist | "Endpoint not found" |
| `:endpoint_disabled` | Endpoint exists but `enabled: false` | "Endpoint is disabled" |
| `{:task_disabled, key}` | The call names a task whose module is switched off | "This AI task's module is switched off, so the task cannot run" |
| `:invalid_endpoint_identifier` | Caller passed a non-UUID value | "Invalid endpoint identifier" |
| `:invalid_api_key` | OpenRouter returned 401 | "Invalid API key" |
| `:insufficient_credits` | OpenRouter returned 402 | "Insufficient credits" |
| `:rate_limited` | OpenRouter returned 429 | "Rate limited" |
| `:request_timeout` | HTTP request timed out | "Request timeout" |
| `{:api_error, status}` | Other non-2xx status | "API error: …" |
| `{:connection_error, reason}` | Transport-level failure | "Connection error: …" |
| `:invalid_json_response` | Response wasn't valid JSON | "Invalid JSON response" |
| `{:prompt_error, :not_found \| :disabled \| :missing_variables}` | Prompt resolution issues | "Prompt …" |
Business logic stays locale-agnostic; the UI calls
`PhoenixKitAI.Errors.message/1` to render the atom as a translated
string via gettext.
```elixir
case PhoenixKitAI.ask(endpoint.uuid, "Hello") do
{:ok, response} ->
{:ok, text} = PhoenixKitAI.extract_content(response)
text
{:error, reason} ->
Logger.warning("AI call failed: #{inspect(reason)}")
{:error, PhoenixKitAI.Errors.message(reason)}
end
```
## Response structure
### Chat completion
```elixir
%{
"id" => "gen-...",
"model" => "anthropic/claude-3-haiku",
"choices" => [
%{
"message" => %{"role" => "assistant", "content" => "..."},
"finish_reason" => "stop"
}
],
"usage" => %{
"prompt_tokens" => 10,
"completion_tokens" => 15,
"total_tokens" => 25,
"cost" => 0.00003
},
"latency_ms" => 850
}
```
### Embeddings
```elixir
%{
"data" => [%{"embedding" => [0.123, -0.456, ...], "index" => 0}],
"usage" => %{"prompt_tokens" => 5, "total_tokens" => 5},
"latency_ms" => 120
}
```
## Cost tracking
A call's cost is stored as it is, in **US dollars** (column `cost`,
`numeric(28,18)`, a `Decimal` in Elixir), so cheap calls keep their cost: a
short embedding at $0.00000004 is `0.00000004`.
```elixir
PhoenixKitAI.Request.format_cost(Decimal.new("0.00000004")) # => "$0.0000000400"
PhoenixKitAI.Request.format_cost(Decimal.new("0.00003")) # => "$0.000030"
PhoenixKitAI.Request.format_cost(Decimal.new("1.5")) # => "$1.50"
```
`format_cost/1` tiers decimal precision: 2 decimals from $0.01, 4 from
$0.0001, 6 from $0.000001, 10 below.
## Project structure
```
lib/
phoenix_kit_ai.ex # Main module (behaviour + the whole public context)
phoenix_kit_ai/
endpoint.ex prompt.ex request.ex # Ecto schemas
completion.ex # Provider-agnostic HTTP client (chat, embeddings, TTS, STT)
providers/ # Adapters per provider key (+ the shared HTTP door, Bedrock Converse)
openrouter_client.ex # Generic provider client (key resolution, model discovery)
images.ex images/ box.ex # Image generation/editing, vision verbs, located answers
transcription.ex # Speech to text (chat path or /audio/transcriptions)
guard.ex guard_blocks.ex # Call-time model/task guard; blocks of people
task_limits.ex task_spec.ex tasks.ex # AI task registry and per-task/per-user limits
budget.ex cache.ex limiter.ex request_log.ex # Spend caps, caches, concurrency, async usage rows
retention.ex journal_prune_worker.ex # Journal retention policy + nightly cleanup job
migrations.ex # The module's own migration chain
routes.ex # Admin sub-routes
web/ # Admin LiveViews: endpoints, endpoint_form, prompts,
# prompt_form, playground, usage, tasks, …
test/
phoenix_kit_ai_test.exs # Behaviour compliance tests
phoenix_kit_ai/ # Unit + integration tests (HTTP stubbed via Req.Test)
support/ # Test infra (TestRepo, DataCase, LiveCase, fixtures)
```
## Database tables
All tables use UUIDv7 primary keys and timestamptz columns. Core 3.x no
longer creates this module's tables — the module's own migration chain
owns them:
- **`phoenix_kit_ai_endpoints`** — endpoint configurations
- **`phoenix_kit_ai_prompts`** — prompt templates
- **`phoenix_kit_ai_requests`** — request logs (FK to endpoints, prompts,
users)
The module's chain (`PhoenixKitAI.Migrations`, run by
`mix phoenix_kit.update` after core's) adopts those three (V5 creates them
exactly as a fresh core 2.x built them, a no-op on existing installs) and
holds the rest:
- **`phoenix_kit_ai_model_capabilities`** — the snapshot of what each
endpoint's model can do (images and documents in, images out, tools,
JSON schema), read from the provider's catalogue
- **`phoenix_kit_ai_task_settings`**, **`phoenix_kit_ai_task_user_limits`**
— the limits of AI tasks and users' personal limits
- **`phoenix_kit_ai_guard_events`** — what the task guard noticed (limits,
models that stopped fitting), deduplicated per window; kept until a
retention period is set for them (forever by default, see "Journal
retention" above)
- **`phoenix_kit_ai_guard_blocks`** — people blocked from a group of AI
tasks, with who set and lifted each block; one active per group and person
It also adds one index to core's `phoenix_kit_ai_requests`:
`((metadata->>'task'), inserted_at)`. It is built inside the migration's
transaction, not concurrently, and AI usage rows wait while it builds: on a
large journal, measure it first and update in a quiet window.
## Admin pages
| Page | Path | Description |
|------|------|-------------|
| Endpoints | `/admin/ai/endpoints` | List, create, edit, delete, validate |
| Endpoint form | `/admin/ai/endpoints/new`, `.../edit` | Create/edit with model selection |
| Prompts | `/admin/ai/prompts` | List, create, edit, delete, reorder |
| Prompt form | `/admin/ai/prompts/new`, `.../edit` | Create/edit with variable extraction |
| Playground | `/admin/ai/playground` | Interactive testing with live variables |
| Usage | `/admin/ai/usage` | Dashboard stats and request history |
| Tasks | `/admin/ai/tasks` | Declared AI tasks, their needs, endpoints and fitness; limits, use, watchers; off-purpose flags and blocks (changes need `ai.manage`) |
## Supported providers
There is no hardcoded provider list in this module: the endpoint form's
provider dropdown is driven by core's `PhoenixKit.Integrations` registry —
every entry with the `:ai_completions` capability works, and adding a
provider is one registry entry in core plus a `base_url`, as long as the
API exposes `<base_url>/chat/completions` and `/models`. The providers core
ships with `:ai_completions`:
| Provider | Default base URL | Notes |
|---|---|---|
| **OpenRouter** | `https://openrouter.ai/api/v1` | Aggregated gateway; `/models` carries pricing + modality metadata, models grouped by underlying provider. Embedding models are listed separately — see below. The only provider `required_integrations/0` asks for |
| **OpenAI** | `https://api.openai.com/v1` | OpenAI-compatible path |
| **Anthropic** | `https://api.anthropic.com/v1` | Its OpenAI-compatible layer; native model list + lifecycle, built-in price table |
| **Mistral** | `https://api.mistral.ai/v1` | `/v1/models` mixes chat and embeddings; `/audio/voices` feeds the TTS voice picker |
| **DeepSeek** | `https://api.deepseek.com/v1` | `/models` carries input/output modalities + context window; reasoner models return chain-of-thought (see Reasoning capture above) |
| **xAI** | `https://api.x.ai/v1` | OpenAI-compatible; also the realtime voice transport |
| **Z.ai** | `https://api.z.ai/api/paas/v4` | OpenAI-compatible |
| **Amazon Bedrock** | `https://bedrock-runtime.<region>.amazonaws.com/openai/v1` | `openai.gpt-oss-*` models on the OpenAI-compatible URL; every other model (e.g. `eu.anthropic.*` inference profiles) goes through the native Converse API (`PhoenixKitAI.Providers.BedrockConverse`). Converse reports no cost — set a manual price before relying on a spend cap |
All OpenAI-compatible providers share the same `Completion.chat_completion/3`
HTTP path; the form's provider dropdown drives the picker filter, default
base URL, and model-list fetcher. Image verbs pick an adapter by provider
key (OpenRouter's unified Images API, xAI, OpenAI multipart, and the
OpenAI-compatible default for the rest) — a new image provider is a
`PhoenixKitAI.Provider` adapter plus a `config :phoenix_kit_ai,
:provider_adapters` entry.
Each provider's connection is set up under **Settings → Integrations**
(those entries live in core's `PhoenixKit.Integrations.Providers`
registry). After validation, the picker on the AI endpoint form
filters connections to whichever provider is currently selected on
the dropdown; switching providers clears any selected integration and
resets `base_url` to the new provider's default.
Models auto-load from the chosen provider's `/models` endpoint when
the integration is picked. Slow fetches (>10s) surface a "still
loading" hint next to the spinner; failed fetches show a Retry button
on the error pane so operators can recover from transient upstream
issues without re-picking the integration.
**Embedding models for OpenRouter** are NOT returned by `/models`;
OpenRouter lists them at `GET /api/v1/embeddings/models`, which
`OpenRouterClient.fetch_embedding_models/2` reads and caches for an hour
(`config :phoenix_kit_ai, embedding_models_ttl:` seconds;
`embedding_models_live: false` turns the request off). When that fetch
fails, a curated list maintained in source is used instead; either list
is extensible via:
```elixir
config :phoenix_kit_ai,
embedding_models: [
%{"id" => "custom/embedding-model", "name" => "Custom",
"context_length" => 8192, "dimensions" => 1024,
"pricing" => %{"prompt" => 0.00000001, "completion" => 0}}
]
```
Mistral exposes embedding models in the same `/v1/models` list as
chat models (`mistral-embed`, `codestral-embed`); operators select
the right one manually. DeepSeek currently exposes chat models only.
## PhoenixKit.Module callbacks
| Callback | Value |
|----------|-------|
| `module_key/0` | `"ai"` |
| `module_name/0` | `"AI"` |
| `enabled?/0` | DB-backed boolean with rescue fallback |
| `enable_system/0` / `disable_system/0` | Persist via Settings API |
| `version/0` | current package version |
| `permission_metadata/0` | key `"ai"`, icon `hero-sparkles` |
| `admin_tabs/0` | Parent + 5 subtabs (endpoints, prompts, playground, usage, tasks) |
| `css_sources/0` | `[:phoenix_kit_ai]` |
| `route_module/0` | `PhoenixKitAI.Routes` |
| `required_integrations/0` | `["openrouter"]` |
| `get_config/0` | `%{enabled, endpoints_count, total_requests, total_tokens}` |
## Development
```bash
mix deps.get # Install dependencies
mix precommit # compile + docs + format + credo --strict + dialyzer
mix test # Run all tests
mix format # Format code
mix credo --strict # Linting
mix dialyzer # Type checking
mix quality # format + credo --strict + dialyzer
```
## Testing
```bash
# Full suite — provisions and migrates its own PostgreSQL database
# (phoenix_kit_ai_test) via the `test` alias; a broken database ABORTS
# the run rather than silently skipping the integration tests
mix test
# LiveView tests (same DB, uses local Test.Endpoint)
mix test test/phoenix_kit_ai/web/
# Unit tests only, on a machine with no Postgres (explicit opt-out —
# never in CI or before a release)
PK_AI_SKIP_DB=1 mix test
```
Integration tests use an embedded `PhoenixKitAI.Test.Repo` with Ecto
sandbox and are tagged `:integration`. That tag is excluded ONLY under the
explicit `PK_AI_SKIP_DB=1` opt-out above — an unreachable or broken test
database makes `test_helper.exs` raise, so a red run can never masquerade
as "0 failures, most tests skipped".
## Troubleshooting
- **Models not loading** — check the OpenRouter integration has a valid
API key in Settings → Integrations, and the account has credits.
- **Slow responses** — use a faster model (Haiku instead of Opus),
reduce `max_tokens`, or check OpenRouter's status page.
- **High costs** — monitor the Usage tab; consider cheaper models and
caching repeated queries.
- **Debug logging** — `Logger.configure(level: :debug)`. Request logs
live in `phoenix_kit_ai_requests` with full caller context.
## Getting help
1. This README for API documentation
2. OpenRouter docs: <https://openrouter.ai/docs>
3. `dev_docs/pull_requests/` for the history of reviewed changes