Token Usage Outbox (storage/token-usage-outbox.ts)

memmy-agent · Memory/src/storage/token-usage-outbox.ts · typescript · MIT
database, analytics, tag:tokens

A SQLite-backed queue for tracking and persisting token consumption events. Use this to reliably record usage metrics before external synchronization.

// Source: https://github.com/MemTensor/memmy-agent | License: MIT | Copyright 2026-present MemTensor
// Provided via justclaw.it.com; keep this notice with the code.
import type Database from "better-sqlite3";

export interface TokenUsageOutboxRecord {
  eventId: string;
  payloadJson: string;
}

export class TokenUsageOutbox {
  constructor(private readonly db: Database.Database) {}

  enqueue(eventId: string, payloadJson: string): void {
    this.db.prepare(
      `INSERT INTO token_usage_outbox (event_id, payload_json) VALUES (?, ?)`
    ).run(eventId, payloadJson);
  }

  listNext(limit: number): TokenUsageOutboxRecord[] {
    const rows = this.db.prepare(
      `SELECT event_id, payload_json
       FROM token_usage_outbox
       ORDER BY sequence ASC
       LIMIT ?`
    ).all(Math.ma

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key