Skip to content

feat(event): add canonical Usage event builder, adapter contract, and dep bump#3

Open
chgeuer wants to merge 3 commits intoagentjido:mainfrom
chgeuer:fix/update-sprite-lifecycle-alias
Open

feat(event): add canonical Usage event builder, adapter contract, and dep bump#3
chgeuer wants to merge 3 commits intoagentjido:mainfrom
chgeuer:fix/update-sprite-lifecycle-alias

Conversation

@chgeuer
Copy link

@chgeuer chgeuer commented Mar 5, 2026

Closes #4

What

Adds Jido.Harness.Event.Usage.build/3 — a builder that constructs canonical :usage events with a standardized flat payload. Also extends AdapterContract to verify that adapters declaring usage?: true emit at least one conforming :usage event.

Canonical payload shape

%{
  "input_tokens"        => non_neg_integer(),   # required
  "output_tokens"       => non_neg_integer(),   # required
  "total_tokens"        => non_neg_integer(),   # computed if absent
  "cached_input_tokens" => non_neg_integer(),   # optional, default 0
  "cost_usd"            => float() | nil,       # optional
  "duration_ms"         => integer() | nil,     # optional
  "model"               => String.t() | nil     # optional
}

Key design decisions:

  • Flat payload (no nested "usage" key) — simpler for consumers to pattern-match
  • One event per session, emitted just before :session_completed
  • session_id required — enables session-level aggregation

Usage

alias Jido.Harness.Event.Usage, as: UsageEvent

UsageEvent.build(:claude, "session-42",
  input_tokens: 1250,
  output_tokens: 890,
  cost_usd: 0.00542,
  model: "claude-sonnet-4-20250514"
)

Changes

  • lib/jido_harness/event/usage.ex — new module with build/3 and @moduledoc documenting the contract
  • lib/jido_harness/adapter_contract.ex — new contract test: adapters with usage?: true must emit :usage events with input_tokens, output_tokens, total_tokens, and a non-nil session_id
  • test/jido_harness/event/usage_test.exs — 8 unit tests covering required/optional fields, defaults, and validation
  • mix.exs — bumps jido dep to ~> 2.1

Test results

67 tests, 0 failures

chgeuer added 3 commits March 5, 2026 09:05
The module was renamed in jido_shell PR #22 (commit b5132be) from
Jido.Shell.SpriteLifecycle to Jido.Shell.Environment.Sprite.

Closes agentjido#2
Add Jido.Harness.Event.Usage.build/3 that produces a standardized :usage
event with flat payload (input_tokens, output_tokens, total_tokens, plus
optional cached_input_tokens, cost_usd, duration_ms, model).

Extend AdapterContract: adapters declaring usage?: true must emit at least
one :usage event with the required payload keys and a non-nil session_id.
@chgeuer chgeuer changed the title fix: update SpriteLifecycle alias to Jido.Shell.Environment.Sprite feat(event): add canonical Usage event builder, adapter contract, and dep bump Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add canonical :usage event contract for adapter consistency

1 participant