// guides · n8n error index

n8n: log the token usage and cost of every OpenAI call (Google Sheets)

Wrap your OpenAI calls in one n8n sub-workflow that retries rate limits, reads the usage block, works out the cost in dollars and adds a row to Google Sheets. Tested Code node included.

Updated 2026-10-16 · about 10 minutes

Still stuck after these steps? Paste your exact n8n error and we'll reproduce it and email you the fix, free →

Steps

  1. Make one sub-workflow for all paid API calls

    Create a workflow that starts with the When Executed by Another Workflow trigger and accepts the fields you send (model, messages). Every workflow that needs OpenAI calls it with an Execute Workflow node instead of calling OpenAI directly, so cost tracking lives in one place.

  2. Call OpenAI with an HTTP Request node

    POST to https://api.openai.com/v1/chat/completions with your OpenAI credential and a JSON body like {"model": "{{ $json.model }}", "messages": {{ JSON.stringify($json.messages) }}}. The raw API response includes a usage block with prompt_tokens and completion_tokens (the Responses API calls them input_tokens and output_tokens). Calling the API directly makes sure you get that block back.

  3. Retry rate limits instead of failing

    In the HTTP Request node's Settings, turn on Retry On Fail (for example 3 tries, 5000 ms apart) so a 429 Too Many Requests gets another go. Set On Error to Continue (using error output) so a call that still fails gets logged as well and doesn't silently kill the parent workflow.

  4. Work out the cost in a Code node

    Add a Code node after both outputs and paste the snippet below. Put your model's current prices per million tokens at the top (copy them from OpenAI's pricing page; the numbers in the snippet are only examples).

    // $ per 1 million tokens for YOUR model. Example numbers only: copy the real ones from openai.com/api/pricing
    const PRICE_IN = 0.40, PRICE_OUT = 1.60;
    return $input.all().map(item => {
      const r = item.json;
      const u = r.usage || {};
      const inTok = u.prompt_tokens ?? u.input_tokens ?? 0;
      const outTok = u.completion_tokens ?? u.output_tokens ?? 0;
      return { json: {
        time: new Date().toISOString(),
        workflow: $workflow.name,
        model: r.model || '',
        input_tokens: inTok,
        output_tokens: outTok,
        cost_usd: Number(((inTok * PRICE_IN + outTok * PRICE_OUT) / 1e6).toFixed(6)),
        error: r.error ? (r.error.message || String(r.error)) : ''
      } };
    });
    

  5. Append a row to Google Sheets, then return the answer

    Add a Google Sheets node (Append Row) with columns time, workflow, model, input_tokens, output_tokens, cost_usd, error. Then return the model's reply to the parent workflow. A pivot table or =SUMIF on the sheet now shows the spend per workflow and per day.

What the snippet does

Good to know

FAQ

Why not use the OpenAI node or AI Agent node?

You can, but what they pass on varies between n8n versions and node options, and the usage block isn't always in the output. A plain HTTP Request node gives you the full API response, so the token counts are always there.

Is the cost exact?

It's as exact as the prices you type in. The token counts come from OpenAI's own response. Compare a week of rows with your OpenAI usage dashboard once to be sure the prices are right.

Does this slow my workflows down?

A little: one extra sub-workflow call and one Google Sheets append per request. For high-volume workflows, log to a database or write rows in batches instead.

Can I stop a workflow when the budget runs out?

Yes. Before the HTTP Request, read today's total from the sheet and stop with a Stop and Error node when it's over your limit.

Related guides

New guides

Still stuck on your n8n error?

Paste the exact error and say what the workflow should do. We reproduce it on a real n8n install and email you the fix or a working workflow, usually within 24h. Free while we're in beta.

We'll only email you about your error and TaskToAgent.