Skip to content
Back to skills

Output Dev Cost Hooks

ASecurity

Subscribe to cost events (onHttpCost, onLLMMetering) to forward LLM and API spend to your own observability system. Use when adding cost/spend logging, building a cost observability integration, or forwarding per-request cost data to an external system.

  • 445 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 20, 2026
ai-agentsjavascripttypescriptgojavaapidatabasebackend

Works with

  • cli
  • api

Security analysis

A100/100

Scanned December 4, 2026

npx -y skills add growthxai/output --skill output-dev-cost-hooks --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Output Dev Cost Hooks?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Output Dev Cost Hooks
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/growthxai-output-dev-cost-hooks/badge)](https://www.skillsdirectory.com/skills/growthxai-output-dev-cost-hooks)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: output-dev-cost-hooks
description: Subscribe to cost events (onHttpCost, onLLMMetering) to forward LLM and API spend to your own observability system. Use when adding cost/spend logging, building a cost observability integration, or forwarding per-request cost data to an external system.
allowed-tools: [Read, Write, Edit, Glob]
---

# Cost Observability Hooks

## Overview

This skill documents how to subscribe to Output's cost events so every priced LLM call and every HTTP call with attached cost (via `addRequestCost`, see `output-dev-http-client-create`) can be forwarded to your own observability system — a webhook, a structured log pipeline, a metrics backend, etc. Handler errors are caught and logged by the framework; they never affect the workflow or the request that triggered them.

## When to Use This Skill

- Forwarding per-call spend to an external observability/webhook endpoint
- Logging cost events as structured fields for a log platform
- Building alerting or dashboards on top of workflow spend
- Auditing which workflows/activities are driving cost, in real time rather than after the fact
- Answering "how much did this workflow run cost?", with a breakdown per model and per API host (see Pattern 3)

This skill is about **project-wide hook registration** for cost data already emitted by the framework. To make an HTTP client emit cost in the first place, see `output-dev-http-client-create`.

## Setup

### 1. Create a hook file

```typescript
// src/cost_hooks.ts
import { onHttpCost, onLLMMetering } from '@outputai/core/hooks';

onHttpCost(async event => {
  // handle HTTP cost
});

onLLMMetering(async event => {
  // handle LLM cost
});
```

### 2. Register the file

Add the file to `outputai.hookFiles` in `package.json`, alongside any existing hook files. Paths are relative to the package root and point at the **built** `.js` output — `npm run output:worker:build` compiles `src/` to `dist/` (per `tsconfig.json`'s `rootDir`/`outDir`), so `src/cost_hooks.ts` is registered as `dist/cost_hooks.js`, not the `.ts` source itself. The worker loads these files at startup.

```json
{
  "outputai": {
    "hookFiles": [
      "./dist/cost_hooks.js"
    ]
  }
}
```

If you'd rather skip the build step, a hook file can also be plain, uncompiled JavaScript registered directly at its `src/` path (`"./src/cost_hooks.js"`) — see `https://docs.output.ai/operations/error-hooks` for that variant. The rest of this skill uses TypeScript + the built-output path, since that's what the framework's own examples (and every other file in a scaffolded project) use.

## Events You Can Subscribe To

| Hook | Payload type | When it fires | Prefer for |
|------|--------------|----------------|-------------|
| `onLLMMetering` | `LLMGenerationMeteringEvent` from `@outputai/core/llm` | After every LLM generation (text, image, Agent, streaming) that reports usage — including failed calls that got at least partial usage | LLM cost integrations |
| `onHttpCost` | `HttpRequestCostEvent` from `@outputai/core/http` | Only when your code (or a client's `afterResponse` hook) calls `addRequestCost(response, total)` | Non-LLM paid API calls |

The hooks are typed, so handlers need no type imports. Do not use `on('llm:generation:metering')`, `on('cost:llm:request')`, or `on('cost:http:request')`: they are deprecated and log a warning. When migrating `on('cost:llm:request')`, note that `onLLMMetering` has a different payload (`payload.cost?.total` instead of `payload.total`).

Every event carries the same envelope: `eventId` (UUID v4, stable idempotency key), `eventDate` (ms epoch), `activityInfo` and `workflowDetails` (present when emitted from within a step/evaluator), `outputActivityKind`, and `payload` (the event-specific data described above).

## Pattern 1: Forward to an external endpoint

Use this when spend needs to reach an external observability system over HTTP. Forward the raw envelope plus payload; redact anything that might carry secrets (API keys or tokens embedded in query strings) before logging or sending the URL.

```typescript
// src/cost_hooks.ts
import { onHttpCost, onLLMMetering } from '@outputai/core/hooks';
import { credentials } from '@outputai/core/credentials';

// Use plain fetch here, not createKyClient/outputFetch. Handlers run inside
// the emitting step's async context, so a traced client would add its own
// HTTP trace event to that step's trace on every forwarded cost event.
//
// Read the credential lazily with `get`, not `require`, at module scope: a
// hook file is imported at worker startup by an unguarded `await import()`
// (there's no try/catch around it), so a `require()` that throws here takes
// the whole worker down. `get`/`require` also only see workflow-scoped
// credentials from inside an activity — at startup, outside any activity,
// they resolve the global credential set only, so keep this in a global
// (not per-workflow) credential file.
const getWebhookUrl = (): string | undefined => credentials.get('observability.webhook_url') as string | undefined;

const postEvent = async (json: Record<string, unknown>): Promise<void> => {
  const webhookUrl = getWebhookUrl();
  if (!webhookUrl) {
    return;
  }

  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 5000);
  try {
    await fetch(`${webhookUrl}/events`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify(json),
      signal: controller.signal
    });
  } finally {
    clearTimeout(timeout);
  }
};

// Strip query strings — some APIs put API keys or tokens there.
const redactUrl = (url: string): string => {
  try {
    const parsed = new URL(url);
    return `${parsed.origin}${parsed.pathname}`;
  } catch {
    return '[unparseable-url]';
  }
};

onHttpCost(async event => {
  if (!event.workflowDetails) {
    return;
  }

  // A caught failure here logs with this handler's own context; an
  // uncaught one is still caught and logged by the framework either way.
  try {
    await postEvent({
      eventId: event.eventId,
      eventDate: event.eventDate,
      workflowId: event.workflowDetails.workflowId,
      kind: 'http',
      url: redactUrl(event.payload.url),
      totalUsd: event.payload.total
    });
  } catch (error) {
    console.warn('cost_hooks: failed to forward HTTP cost event', error);
  }
});

onLLMMetering(async event => {
  if (!event.workflowDetails) {
    return;
  }

  try {
    await postEvent({
      eventId: event.eventId,
      eventDate: event.eventDate,
      workflowId: event.workflowDetails.workflowId,
      kind: 'llm',
      callId: event.payload.callId,
      providerId: event.payload.usage.providerId,
      modelId: event.payload.usage.modelId,
      promptName: event.payload.usage.promptName,
      requestIds: event.payload.usage.requestDetails?.ids ?? [],
      durationMs: event.payload.usage.requestDetails?.duration,
      totalUsd: event.payload.cost?.total ?? null
    });
  } catch (error) {
    console.warn('cost_hooks: failed to forward LLM cost event', error);
  }
});
```

## Pattern 2: Structured logging

Use this when spend just needs to land in your log platform as structured facets, without a separate network call. Use the framework `Logger` so fields are emitted consistently with the rest of the worker's logs.

```typescript
// src/cost_hooks.ts
import { onHttpCost, onLLMMetering } from '@outputai/core/hooks';
import { Logger } from '@outputai/core';

const log = Logger.createLogger('CostObservability');

const redactUrl = (url: string): string => {
  try {
    const parsed = new URL(url);
    return `${parsed.origin}${parsed.pathname}`;
  } catch {
    return '[unparseable-url]';
  }
};

onHttpCost(async event => {
  if (!event.workflowDetails) {
    return;
  }

  log.info('http_request_cost', {
    eventId: event.eventId,
    workflowId: event.workflowDetails.workflowId,
    url: redactUrl(event.payload.url),
    totalUsd: event.payload.total
  });
});

onLLMMetering(async event => {
  if (!event.workflowDetails) {
    return;
  }

  log.info('llm_generation_cost', {
    eventId: event.eventId,
    workflowId: event.workflowDetails.workflowId,
    callId: event.payload.callId,
    providerId: event.payload.usage.providerId,
    modelId: event.payload.usage.modelId,
    promptName: event.payload.usage.promptName,
    requestIds: event.payload.usage.requestDetails?.ids ?? [],
    durationMs: event.payload.usage.requestDetails?.duration,
    totalUsd: event.payload.cost?.total ?? null
  });
});
```

## Pattern 3: Total cost per workflow run

Use this when you need the cost of one workflow run: a total, plus a breakdown per model and per API host. Emit one flat record per cost event, keyed by run, and let the system that receives the records add them up (a `SUM ... GROUP BY` in your log platform, a counter in your metrics backend, or rows in a database).

Do not keep running totals in memory inside the hook. The steps of one run can execute on different workers, a worker can restart mid-run, and retried attempts arrive as separate events, so an in-process map never holds the whole run.

```typescript
// src/cost_hooks.ts
import { onHttpCost, onLLMMetering } from '@outputai/core/hooks';

// USD per million tokens, for models the pricing catalog does not know
// (fine-tunes, self-hosted models). Catalog-priced models do not need an entry.
const fallbackRates: Record<string, { input: number; output: number }> = {
  'acme-ft-summarizer': { input: 3, output: 15 }
};

const priceFromUsage = (modelId: string, input: number | null, output: number | null): number | null => {
  const rate = fallbackRates[modelId];
  if (!rate) {
    return null;
  }
  return ((input ?? 0) * rate.input + (output ?? 0) * rate.output) / 1_000_000;
};

onLLMMetering(async event => {
  if (!event.workflowDetails) {
    return;
  }

  const { usage, cost } = event.payload;
  const { root, runId, workflowType } = event.workflowDetails;

  console.log(JSON.stringify({
    event: 'run_cost',
    eventId: event.eventId,
    // A child workflow has the same root as its parent: group by rootRunId to
    // include child workflows in the parent's total, or by runId for one run alone.
    rootRunId: root?.runId ?? runId,
    runId,
    workflowType,
    kind: 'llm',
    target: usage.modelId,
    totalUsd: cost?.total ?? priceFromUsage(usage.modelId, usage.input, usage.output),
    costStatus: cost?.status ?? 'unpriced'
  }));
});

onHttpCost(async event => {
  if (!event.workflowDetails) {
    return;
  }

  const { root, runId, workflowType } = event.workflowDetails;

  console.log(JSON.stringify({
    event: 'run_cost',
    eventId: event.eventId,
    rootRunId: root?.runId ?? runId,
    runId,
    workflowType,
    kind: 'http',
    target: new URL(event.payload.url).host,
    totalUsd: event.payload.total,
    costStatus: 'precise'
  }));
});
```

Each record is one JSON line on stdout, which most log platforms parse into fields. Use plain logs here rather than the framework `Logger`: `Logger` is meant for workflow and step code, and it drops reserved metadata names such as `runId` and `workflowType`.

Then, in the receiving system:

- **Run total**: sum `totalUsd` where `rootRunId` is the run you are looking at.
- **Breakdown**: group the same sum by `kind` and `target` (model ID for LLM calls, host for HTTP calls).
- **Completeness**: a run total is only a lower bound when any of its records has `costStatus` `incomplete` or `unpriced`, or a `null` `totalUsd`. Report those records next to the total instead of hiding them.
- **Deduplication**: use `eventId` as the unique key if records can be delivered twice.

Price adjustments live in the hook now:

- **Unpriced LLM calls**: `payload.cost` is missing or has a `null` total when the model is not in the pricing catalog. Add the model to `fallbackRates` (or register the provider with `extractStepCost`, see the LLM docs) instead of leaving it at `null`.
- **HTTP calls**: the hook only sees what the client attached with `addRequestCost`. To change a service's price, change the client (see `output-dev-http-client-create`), not the hook.

## Handler Safety Rules

- **Handlers are wrapped in try/catch by the framework.** A thrown or rejected handler is caught and logged (e.g. `<eventName> hook error`) and never affects the workflow, the worker, or the request that triggered the event — the same guarantee `onError` handlers get. Adding your own `try/catch` is still worthwhile so a forwarding failure logs with context you control, but it's not required for safety.
- **Skip events outside a workflow.** Guard on `event.workflowDetails` before using it — it's only populated when the event was emitted from within a step/evaluator context; `eventId`, `eventDate`, and `payload`, by contrast, are always present.
- **Redact before logging or sending URLs.** Some third-party APIs embed API keys or tokens in query strings. Strip the query string (and any path segments you know carry secrets) before it leaves the process.
- **Use `eventId` as an idempotency key** when forwarding to a system that might receive the same event more than once (e.g. retried delivery).
- **Expect one event per attempt.** A step or evaluator that retries emits one cost event per attempt — that's correct, since each attempt is a real billed call.
- **Group by prompt and correlate with the provider using `requestDetails`.** `payload.usage` and `payload.cost` both carry `promptName` and `requestDetails` (`ids`, `count`, `duration` in milliseconds). `ids` holds the provider request IDs in request order and is empty when the provider exposes none (for example Google Vertex), so do not assume `ids.length === count`. Both fields are optional in the types because traces recorded before they existed lack them; events always carry them.
- **Correlate with the call using `callId`.** `onLLMMetering` and `onLLMRequest` (which carries the prompt and the response) share the same `payload.callId` for a call.

## Verification Checklist

- [ ] Hook file registered in `outputai.hookFiles` in `package.json`, pointing at the **built** `.js` path (`dist/...`), not the `.ts` source
- [ ] Hook file imports `onHttpCost` / `onLLMMetering` from `@outputai/core/hooks`, not the deprecated `on('cost:http:request')`, `on('llm:generation:metering')`, or `on('cost:llm:request')`
- [ ] Handlers guard on missing `workflowDetails`
- [ ] URLs are redacted (query string stripped) before logging or forwarding
- [ ] Per-run totals are summed in the receiving system, keyed by `rootRunId` or `runId`, not kept in worker memory

## Related Skills

- `output-dev-http-client-create` - Attaching cost to a paid API client with `addRequestCost`
- `output-dev-credentials` - Storing the observability endpoint URL/token as a credential

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.