{
  "id": "near-route-check",
  "name": "Taifoon × NEAR AI Cloud — one routed call, as NEAR's router makes it, and its settlement line",
  "nodes": [
    {
      "parameters": {
        "content": "## Taifoon as a provider of an inference cloud: one routed call\n**For:** either side of an inference cloud integration that wants to see one routed call and its settlement line.\n**Why:** it lists Taifoon's models, makes one chat completion as the cloud's router would, and writes the settlement line: cost = prompt tokens × input price + completion tokens × output price, with the completion ID as evidence.\n\n**Set up**\n1. Get a free key: the node's **Account → Register Free Key**, template 11, or `curl -s -X POST https://coord.taifoon.dev/v1/register -H 'Content-Type: application/json' -d '{}'` (the key is shown once).\n2. Add a **Taifoon Relayer API** credential with the key (Credentials → New) and select it on the Taifoon nodes that ask for one. Select it on **List models** and **One call**.\n\n**Test it:** execute the workflow. **Settlement line** shows the model, the token counts, the cost in micro-USD and the evidence. Without n8n: `curl -s https://coord.taifoon.dev/v1/chat/completions -H 'Content-Type: application/json' -H \"X-API-Key: $TAIFOON_KEY\" -d '{\"messages\":[{\"role\":\"user\",\"content\":\"Say hi\"}],\"max_tokens\":16}'`.\n\n**Limits:** a free key answers about 10 chats a UTC day from one calling address; past that a key with a balance pays per token.\n\n**Needs the community node** `n8n-nodes-taifoon` (tested with 2.1.0): self-hosted n8n → Settings → Community nodes → Install → `n8n-nodes-taifoon`. https://www.npmjs.com/package/n8n-nodes-taifoon\n**Tested:** n8n 2.45.5 with n8n-nodes-taifoon 2.1.0, 2026-11-15: imported from this file into a fresh n8n and run against coord.taifoon.dev.",
        "width": 680,
        "height": 654
      },
      "id": "near-route-check-start-here",
      "name": "Start here",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -760,
        180
      ]
    },
    {
      "parameters": {},
      "id": "start",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "resource": "inference",
        "operation": "listModels"
      },
      "id": "list",
      "name": "List models",
      "type": "n8n-nodes-taifoon.taifoon",
      "typeVersion": 1,
      "position": [
        220,
        300
      ],
      "notes": "GET /v1/models with the key: the models the gate serves at the list price (the same list the registry discovers).",
      "notesInFlow": true,
      "credentials": {
        "taifoonRelayerApi": {
          "id": "",
          "name": "Taifoon Relayer API"
        }
      }
    },
    {
      "parameters": {
        "resource": "inference",
        "operation": "chat",
        "model": "llama-3.3-70b-instruct",
        "messages": "[{\"role\":\"user\",\"content\":\"Reply with the single word: routed\"}]",
        "chatOptions": {
          "maxTokens": 8
        }
      },
      "id": "chat",
      "name": "One call",
      "type": "n8n-nodes-taifoon.taifoon",
      "typeVersion": 1,
      "position": [
        440,
        300
      ],
      "notes": "The call NEAR's router makes: model = providerConfig.model_name of a row workflow 20 wrote (the id on Taifoon's list).",
      "notesInFlow": true,
      "credentials": {
        "taifoonRelayerApi": {
          "id": "",
          "name": "Taifoon Relayer API"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// The cost of one routed call on both sides. Taifoon charges the list price on the key (taifoon.usd_per_1m, USD per 1M tokens);\n// NEAR records the call in its own usage at the price its row carries (the same numbers when written by workflow 20).\n// metered: the meter line Taifoon recorded for this call (taifoon.meter: provider and our_fee legs, its step at GET /v1/gw/steps/<step>);\n// legs_sum_to_list and list_matches_charge say whether the recorded legs add up to what the key was charged.\n// settled: what the key paid against the venue's own token counts at the list price (usage_micro = ⌈prompt × in + completion × out⌉, at\n// least 1); charge_matches_usage says the charge is that, not the max_tokens reserve (Taifoon settles a keyed call to its usage).\nconst r = $('One call').first().json; const list = $('List models').first().json.data ?? [];\n// the answer names the venue's model (r.model); the row is found by its listing (gpu:<id>:<venue>), then by id or venue model\nconst m = list.find((x) => r.taifoon?.listing && x.taifoon?.listing === r.taifoon.listing) ?? list.find((x) => x.id === r.model || x.taifoon?.model_identity?.venue_model === r.model) ?? {}; const p = m.taifoon?.usd_per_1m ?? {}; const inP = Number(p.in ?? 0), outP = Number(p.out ?? 0);\nconst u = r.usage ?? {}; const cost = ((u.prompt_tokens ?? 0) * inP + (u.completion_tokens ?? 0) * outP) / 1e6;\nconst sale = r.taifoon?.sale ?? null; const mt = r.taifoon?.meter ?? null;\nconst leg = (to) => (mt?.legs ?? []).filter((l) => l.to === to).reduce((s, l) => s + Number(l.micro ?? 0), 0);\nconst metered = mt ? { step: mt.step ?? null, url: mt.url ?? null, served_by: r.taifoon?.served_by ?? null, list_usdc: Number(mt.list_micro ?? 0) / 1e6,\n  provider_usdc: leg('provider') / 1e6, our_fee_usdc: leg('our_fee') / 1e6, reseller_markup_usdc: leg('reseller_markup') / 1e6,\n  legs_sum_to_list: (mt.legs ?? []).reduce((s, l) => s + Number(l.micro ?? 0), 0) === Number(mt.list_micro ?? -1),\n  list_matches_charge: sale ? Number(mt.list_micro) === Number(sale.micro ?? 0) + Number(sale.markup_micro ?? 0) : null } : null;\nconst usageMicro = Math.max(1, Math.ceil((u.prompt_tokens ?? 0) * inP + (u.completion_tokens ?? 0) * outP));\nconst st = sale?.settled ?? null;\nconst settled = sale ? { usage_micro: usageMicro, charged_micro: Number(sale.micro ?? 0), basis: st?.basis ?? null, reserved_micro: st ? Number(st.reserved_micro) : null,\n  returned_micro: st ? Number(st.returned_micro) : null, charge_matches_usage: Math.abs(Number(sale.micro ?? 0) - usageMicro) <= 1 } : null;\nreturn [{ json: { model: r.model, answer: r.choices?.[0]?.message?.content ?? null, usage: u, list_price_usd_per_mtok: { in: inP, out: outP }, cost_usd_at_list: Number(cost.toFixed(9)), charged_by_taifoon: sale ? { usdc: sale.usdc, sale_id: sale.id } : null, metered, settled, evidence: sale?.id ?? r.id ?? null } }];"
      },
      "id": "line",
      "name": "Settlement line",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        300
      ],
      "notes": "The line NEAR posts to its own settlement endpoint, computed from the usage the answer carries and the list price.",
      "notesInFlow": true
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "List models",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List models": {
      "main": [
        [
          {
            "node": "One call",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One call": {
      "main": [
        [
          {
            "node": "Settlement line",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "all",
    "saveManualExecutions": true
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
