Files
arcadia-cloud/priv/repo/migrations/20260521000000_create_llm_usage_events.exs
Giuliano Silvestro e1f0aedcf7 Receive llm_usage_recorded events from arcadia-llm-gateway
POST /api/v1/integrations/llm-usage stores priced LLM usage events
(idempotent on gateway_request_id) in llm_usage_events. The gateway is
the LLM-pricing authority — arcadia-cloud trusts the charge it sends
rather than re-pricing.

The monthly invoice rollup now appends an llm_usage line per deployment
alongside the infra quote lines; the exact decimal charges are summed
and rounded to cents once. Closes the gateway→cloud billing loop.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 08:29:36 +10:00

40 lines
1.5 KiB
Elixir

defmodule ArcadiaCloud.Repo.Migrations.CreateLlmUsageEvents do
use Ecto.Migration
def change do
# `llm_usage_recorded` events received from arcadia-llm-gateway. The
# gateway is the LLM-pricing authority — it sends the already-priced
# customer charge; arcadia-cloud stores it and rolls it into the
# tenant's monthly invoice. Idempotent on gateway_request_id so a
# gateway retry never double-bills.
create table(:llm_usage_events, primary_key: false) do
add :id, :binary_id, primary_key: true
add :gateway_request_id, :string, null: false
add :tenant_id, :string, null: false
add :deployment_id, :string
add :provider, :string
add :model, :string
add :request_kind, :string
add :input_tokens, :integer, null: false, default: 0
add :output_tokens, :integer, null: false, default: 0
add :cached_input_tokens, :integer, null: false, default: 0
add :total_tokens, :integer, null: false, default: 0
add :upstream_cost, :decimal
add :customer_charge, :decimal
add :customer_charge_cents, :integer, null: false, default: 0
add :markup_mode, :string
add :occurred_at, :utc_datetime_usec, null: false
timestamps(type: :utc_datetime, updated_at: false)
end
create unique_index(:llm_usage_events, [:gateway_request_id])
create index(:llm_usage_events, [:tenant_id, :occurred_at])
create index(:llm_usage_events, [:deployment_id])
end
end