Files
arcadia-cloud/lib/arcadia_cloud/billing/llm_usage_event.ex
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

44 lines
1.3 KiB
Elixir

defmodule ArcadiaCloud.Billing.LlmUsageEvent do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "llm_usage_events" do
field :gateway_request_id, :string
field :tenant_id, :string
field :deployment_id, :string
field :provider, :string
field :model, :string
field :request_kind, :string
field :input_tokens, :integer, default: 0
field :output_tokens, :integer, default: 0
field :cached_input_tokens, :integer, default: 0
field :total_tokens, :integer, default: 0
field :upstream_cost, :decimal
field :customer_charge, :decimal
field :customer_charge_cents, :integer, default: 0
field :markup_mode, :string
field :occurred_at, :utc_datetime_usec
timestamps(type: :utc_datetime, updated_at: false)
end
@required ~w(gateway_request_id tenant_id occurred_at)a
@optional ~w(deployment_id provider model request_kind input_tokens
output_tokens cached_input_tokens total_tokens upstream_cost
customer_charge customer_charge_cents markup_mode)a
def changeset(event, attrs) do
event
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> unique_constraint(:gateway_request_id)
end
end