Skip to main content

wait_for_token_usage_change

Function wait_for_token_usage_change 

Source
pub async fn wait_for_token_usage_change(
    pool: &Pool,
    target_aih: &str,
    baseline: Option<i64>,
) -> Result<i64, Error>
Expand description

Block until this AIH’s stored total_tokens differs from baseline, returning the new value.

Attaches the agent_token_usage_changed listener BEFORE the first read, so a write that lands between the caller’s baseline read and this call is still observed (the post-attach re-read catches it) — no lost wakeup. A real change is always Some (rows are never deleted), so any difference from baseline yields the new value; same-value overwrites (the writer’s upsert fires the trigger even when the number is unchanged) compare equal and keep waiting.