FlowPrime guides

AI: call language models through one gateway, and see what they cost

The AI gateway gives your workspace one OpenAI-compatible endpoint in front of the model providers you register (OpenAI, OpenRouter, Google, Anthropic, or any OpenAI-compatible server). Your product calls a route alias such as chat-default; the gateway picks the provider and model, falls back when one fails, and writes every call to a usage ledger with tokens, cost and latency. Use it when your product calls an LLM and you need the cost per customer or per run, or a public chat bot for a website.

What you can do

Capability How Status
Register a provider account (your own API key) dashboard (AI > Providers) dashboard
Define routes: alias to ordered provider/model targets with fallback dashboard (AI > Routes) dashboard
Try a route live dashboard (AI > Playground) dashboard
Chat completions, streamed or not REST POST /v1/chat/completions (OpenAI shape, model = route alias) live
List the route aliases a caller may use REST GET /v1/models live
Public website chat bot bound to one route and system prompt dashboard (AI > Website bots) to create; REST POST /v1/bot/{slug}/chat to call live
Cost totals for a window, by route, model, client tag and day ai_usage_summary live
The usage ledger, one row per call ai_usage_records live
Your workspace's own ffai_ gateway key not handed out planned

Before you start

  1. The person sets up the gateway in the dashboard, AI page:
  2. Pick the credential your product will call with:
  3. Host: https://{org}.ai.fastflow.tech in production, https://{org}.ai.fastflowtech.ai on staging. Send X-Org-Slug: {org} as well. {org} is your workspace slug (auth_get_org).

Step by step

Get a backend token

  1. Once: auth_create_oidc_app name="Example backend" surface=platform grantTypes=client_credentials. The client secret is shown once: store id and secret as server secrets.
  2. Read token_endpoint from the discovery document whose URL auth_get_org returns in endpoints.discovery.
  3. POST to it, form-encoded: grant_type=client_credentials&client_id=...&client_secret=.... The answer's access_token lasts expires_in seconds (60 minutes). Cache it and fetch a new one shortly before it ends; do not fetch one per call.

Call a model from your backend

  1. GET /v1/models with Authorization: Bearer <token> and X-Org-Slug lists the aliases. Use one that exists.
  2. Call it with any OpenAI SDK pointed at the gateway, or plainly:
    curl -s https://{org}.ai.fastflow.tech/v1/chat/completions \
      -H "Authorization: Bearer <token>" -H "X-Org-Slug: {org}" \
      -H "X-FF-Client: myapp:summary:acct_42:" -H "Content-Type: application/json" \
      -d '{"model":"chat-default","messages":[{"role":"user","content":"Summarize this ..."}],"stream":false}'
    
    With the OpenAI Python SDK:
    from openai import OpenAI
    client = OpenAI(base_url=f"https://{org}.ai.fastflow.tech/v1", api_key=token,
                    default_headers={"X-Org-Slug": org})
    reply = client.chat.completions.create(
        model="chat-default",
        messages=[{"role": "user", "content": "Summarize this ..."}],
        user=f"myapp:summary:{account_id}:")   # the client tag
    print(reply.choices[0].message.content, reply.usage)
    
    The SDK sends the token as Authorization: Bearer, which is what the gateway expects.
  3. "stream": true passes the provider's server-sent events through, with usage captured from the final chunk.
  4. Tag every call with a client tag: the OpenAI user field, or the X-FF-Client header when user is absent. Build it as a prefix you can query later, e.g. myapp:<feature>:<customer>:<run>:. Only backend credentials (team or client-credentials tokens) set the tag. A customer's own token cannot: its user field and X-FF-Client are ignored for the ledger, which books the call as account:<account id> (its seat), else user:<sub>.

Know what a run or a customer cost

  1. ai_usage_summary days=7 clientPrefix="myapp:summary:acct_42:" answers requests, tokens, costUsd, average latency, errors, fallbacks and abandoned calls, with breakdowns by route, model, client tag and day, all narrowed to that prefix.
  2. ai_usage_records clientPrefix="myapp:summary:acct_42:" limit=50 lists the calls themselves, newest first, with total for paging.
  3. Use those numbers to bill per use or to enforce an allowance in your product.

Put a chat bot on a website

  1. In the dashboard, AI > Website bots: choose a slug, the route, the system prompt and a requests-per-minute limit per visitor (default 20).
  2. From the page:
    fetch("https://ai.fastflow.tech/v1/bot/<slug>/chat", {
      method: "POST", headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ sessionId: "s_1", messages: [{ role: "user", content: "Do you ship to Portugal?" }] })
    });
    
    No credential: the bot is bound to your workspace, route and prompt on the server. Over the limit it answers 429 rate_limited.

Answers you will see

Answer Meaning Do
400 body.model must carry a route alias no model send a route alias
404 Unknown route '...' no route with that alias GET /v1/models, or create it in the dashboard
409 route has no enabled targets every target of the route is off enable a target in the dashboard
403 Token org does not match target org X-Org-Slug names another workspace send your own slug
403 customer_only a customer token on a team route (routes, providers, usage) use the backend token
429 rate_limited a website bot's per-visitor limit slow down, or raise the bot's limit

Things that trip agents up

Reference

Where to go next