# Track OpenAI Job Tokens, Cost, And Latency With One POST

Add one TelemHQ ping after an OpenAI job runs so you can see model, token usage, estimated cost, latency, processed items, failures, and quality checks in run history.

## 1. Create A Tracker

Create a TelemHQ tracker for the OpenAI job.

Add a cron schedule if the job should run on time. Leave the schedule blank for queue workers, agents, and manually triggered jobs.

Copy the tracker ping URL:

```text
https://telemhq.com/ping/YOUR_TOKEN
```

## 2. Send Operational Metadata

Avoid sending prompts, completions, retrieved documents, or customer content unless you intentionally want them in history.

Recommended payload:

```json
{
  "provider": "openai",
  "model": "gpt-5",
  "project": "support-digest",
  "status": "success",
  "input_tokens": 8120,
  "output_tokens": 940,
  "total_tokens": 9060,
  "latency_ms": 1840,
  "cost_usd": 0.42,
  "items_processed": 128,
  "items_failed": 0,
  "eval_score": 0.91
}
```

## 3. Add The Ping In Node.js

```js
import OpenAI from "openai";

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const model = process.env.OPENAI_MODEL || "gpt-5";
const startedAt = Date.now();

try {
  const response = await client.responses.create({
    model,
    input: "Summarize today's support tickets."
  });

  const usage = response.usage || {};

  await fetch(process.env.TELEMHQ_PING_URL, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      provider: "openai",
      model: response.model || model,
      project: "support-digest",
      status: "success",
      input_tokens: usage.input_tokens || 0,
      output_tokens: usage.output_tokens || 0,
      total_tokens: usage.total_tokens || 0,
      latency_ms: Date.now() - startedAt,
      items_processed: 1,
      items_failed: 0
    })
  });
} catch (error) {
  await fetch(process.env.TELEMHQ_PING_URL, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      provider: "openai",
      model,
      project: "support-digest",
      status: "error",
      latency_ms: Date.now() - startedAt,
      items_processed: 0,
      items_failed: 1,
      error: error instanceof Error ? error.message : "Unknown error"
    })
  });

  throw error;
}
```

## 4. Add Payload Assertions

Assertions catch silent failures: jobs that technically finish but report bad run data.

Useful checks:

- `status = success`
- `items_processed > 0`
- `items_failed = 0`
- `cost_usd <= your budget`
- `eval_score >= your quality threshold`

Full OpenAI guide: https://telemhq.com/docs/openai
