{"id":"ai.tokens.count","module":"ai-runtime","namespace":"ai","action":"tokens.count","method":"POST","path":"/v1/ai/tokens/count","idempotent":false,"available":true,"vendors":[],"vendors_ready":[],"vendors_pending":[],"key_status":"live","default_vendor":null,"self_hosted":true,"minimum_tier":"standard","supported_message_classes":null,"regions":["western","china"],"dynamic_params":{},"models_tracked":false,"sdk_hint":"Regular parameters + types are stable — read them from the typed SDK signature / API reference for this capability. discovery only carries the runtime-dynamic values above (available, vendors, dynamic_params).","params":{"title":"TokenCountRequest","description":"Request shape for ai.tokens.count. Counts prompt tokens (incl. tool-definition overhead) before sending. Read-only / no network. See docs/Infrai_SDK_AI_Runtime.md §1.5.","type":"object","required":["messages"],"additionalProperties":false,"properties":{"messages":{"type":"array","minItems":1,"items":{"title":"ChatMessage","type":"object","required":["role"],"additionalProperties":false,"properties":{"role":{"enum":["system","user","assistant","tool"],"description":"Role of the message author (system, user, assistant, tool)"},"content":{"oneOf":[{"type":"string"},{"type":"array"},{"type":"null"}],"description":"DNS record value/content"},"name":{"type":"string","description":"Human-readable name for this resource"},"tool_calls":{"type":"array","items":{"title":"ToolCall","type":"object","required":["id","type","function"],"additionalProperties":false,"properties":{"id":{"type":"string","description":"Unique identifier for this resource"},"type":{"const":"function","description":"Type discriminator for this resource"},"function":{"type":"object","required":["name","arguments"],"additionalProperties":false,"properties":{"name":{"type":"string"},"arguments":{"type":"string","description":"JSON-encoded string."}},"description":"Function definition for tool calling"}}},"description":"Tool/function calls requested by the model"},"tool_call_id":{"type":"string","description":"Present when role='tool'."}}},"description":"Chat messages to tokenize (same shape as ai.chat), including tool-definition overhead."},"model":{"type":["string","null"],"description":"Tokenizer model; null = default. Determines which encoding is used."},"tools":{"type":["array","null"],"items":{"type":"object"},"description":"Tool definitions; their serialized overhead is included in the count."}}},"response":{"title":"TokenCountResult","description":"Result payload for ai.tokens.count. See docs/Infrai_SDK_AI_Runtime.md §1.5.","type":"object","required":["prompt_tokens","model"],"additionalProperties":false,"properties":{"prompt_tokens":{"type":"integer","minimum":0,"description":"Total prompt tokens incl. tool-definition overhead."},"model":{"type":"string","description":"Tokenizer model the count was computed for."}}},"errors":["ACCOUNT_AUTORECHARGE_LIMIT","ACCOUNT_FROZEN","AUTH_RATE_LIMIT","AUTH_REFRESH_TOO_FREQUENT","BATCH_REJECTED","CAPABILITY_DEGRADED_NOT_ALLOWED","CONTENT_FILTERED","CONTEXT_WINDOW_EXCEEDED","FAILOVER_COST_EXCEEDED","IDEMPOTENCY_KEY_CONFLICT","INSUFFICIENT_CREDIT","INTERNAL_ERROR","INVALID_ARGUMENT","INVALID_MESSAGE_SCHEMA","INVALID_RESPONSE_FORMAT","INVALID_TOOL_SCHEMA","KEY_REVOKED","MAINTENANCE","MODEL_DEPRECATED","MODEL_NOT_AVAILABLE","MODEL_NOT_FOUND","MODEL_NO_IMAGE_INPUT","MODEL_QUOTA_EXCEEDED","NETWORK_ERROR","NO_HEALTHY_KEY","NO_HEALTHY_VENDOR","RATE_LIMIT_ACCOUNT","RATE_LIMIT_USER","RATE_LIMIT_VENDOR","SCOPE_INSUFFICIENT","STREAM_INTERRUPTED","UNAUTHORIZED","VENDOR_AUTH_ERROR","VENDOR_DOWN","VENDOR_KEY_DUPLICATE","VENDOR_KEY_INVALID","VENDOR_KEY_NOT_FOUND","VENDOR_NOT_CONFIGURED","VENDOR_REQUEST_FAILED","VENDOR_REQUEST_INVALID","VENDOR_RESPONSE_INVALID","VENDOR_TIMEOUT","WALLET_EXPIRED"],"examples":{"curl":"curl -X POST https://api.infrai.cc/v1/ai/tokens/count \\\n  -H \"Authorization: Bearer $INFRAI_API_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"messages\": [{\"role\": \"system\"}]}'","python":"# Zero-install REST call — no SDK required (short-term the API is REST-only).\nimport os, requests\n\nresp = requests.post(\n    \"https://api.infrai.cc/v1/ai/tokens/count\",\n    headers={\n        \"Authorization\": f\"Bearer {os.environ['INFRAI_API_KEY']}\",\n    },\n    json={'messages': [{'role': 'system'}]},\n)\nresp.raise_for_status()\nprint(resp.json())","javascript":"// Zero-install REST call — no SDK required (short-term the API is REST-only).\nconst resp = await fetch(\n  \"https://api.infrai.cc/v1/ai/tokens/count\",\n  {\n    method: \"POST\",\n    headers: {\n      \"Authorization\": `Bearer ${process.env.INFRAI_API_KEY}`,\n      \"Content-Type\": \"application/json\",\n    },\n    body: JSON.stringify({\"messages\": [{\"role\": \"system\"}]}),\n  },\n);\nconsole.log(await resp.json());","typescript":"// Zero-install REST call — no SDK required (short-term the API is REST-only).\nconst resp = await fetch(\n  \"https://api.infrai.cc/v1/ai/tokens/count\",\n  {\n    method: \"POST\",\n    headers: {\n      \"Authorization\": `Bearer ${process.env.INFRAI_API_KEY}`,\n      \"Content-Type\": \"application/json\",\n    },\n    body: JSON.stringify({\"messages\": [{\"role\": \"system\"}]}),\n  },\n);\nif (!resp.ok) throw new Error(`infrai ${resp.status}`);\nconst data: unknown = await resp.json();\nconsole.log(data);","go":"// Zero-install REST call — no SDK required (short-term the API is REST-only).\npackage main\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"net/http\"\n\t\"os\"\n)\n\nfunc main() {\n\tbody := []byte(`{\"messages\": [{\"role\": \"system\"}]}`)\n\treq, _ := http.NewRequest(\"POST\", \"https://api.infrai.cc/v1/ai/tokens/count\", bytes.NewReader(body))\n\treq.Header.Set(\"Authorization\", \"Bearer \"+os.Getenv(\"INFRAI_API_KEY\"))\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\tresp, err := http.DefaultClient.Do(req)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer resp.Body.Close()\n\tfmt.Println(resp.Status)\n}","java":"// Zero-install REST call — no SDK required (short-term the API is REST-only).\nimport java.net.URI;\nimport java.net.http.*;\n\nHttpRequest req = HttpRequest.newBuilder()\n    .uri(URI.create(\"https://api.infrai.cc/v1/ai/tokens/count\"))\n    .header(\"Authorization\", \"Bearer \" + System.getenv(\"INFRAI_API_KEY\"))\n    .header(\"Content-Type\", \"application/json\")\n    .method(\"POST\", HttpRequest.BodyPublishers.ofString(\"{\\\"messages\\\": [{\\\"role\\\": \\\"system\\\"}]}\"))\n    .build();\nHttpResponse<String> resp = HttpClient.newHttpClient()\n    .send(req, HttpResponse.BodyHandlers.ofString());\nSystem.out.println(resp.body());","csharp":"// Zero-install REST call — no SDK required (short-term the API is REST-only).\nusing System;\nusing System.Net.Http;\n\nvar client = new HttpClient();\nvar req = new HttpRequestMessage(new HttpMethod(\"POST\"), \"https://api.infrai.cc/v1/ai/tokens/count\");\nvar key = Environment.GetEnvironmentVariable(\"INFRAI_API_KEY\");\nreq.Headers.Add(\"Authorization\", \"Bearer \" + key);\nreq.Content = new StringContent(\"{\\\"messages\\\": [{\\\"role\\\": \\\"system\\\"}]}\", System.Text.Encoding.UTF8, \"application/json\");\nvar resp = await client.SendAsync(req);\nConsole.WriteLine(await resp.Content.ReadAsStringAsync());","php":"<?php\n// Zero-install REST call — no SDK required (short-term the API is REST-only).\n$ch = curl_init(\"https://api.infrai.cc/v1/ai/tokens/count\");\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, \"POST\");\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, [\n    \"Authorization: Bearer \" . getenv(\"INFRAI_API_KEY\"),\n    \"Content-Type: application/json\",\n]);\ncurl_setopt($ch, CURLOPT_POSTFIELDS, \"{\\\"messages\\\": [{\\\"role\\\": \\\"system\\\"}]}\");\n$response = curl_exec($ch);\ncurl_close($ch);\necho $response;","ruby":"# Zero-install REST call — no SDK required (short-term the API is REST-only).\nrequire \"net/http\"\nrequire \"uri\"\n\nuri = URI(\"https://api.infrai.cc/v1/ai/tokens/count\")\nhttp = Net::HTTP.new(uri.host, uri.port)\nhttp.use_ssl = true\nreq = Net::HTTP::Post.new(uri)\nreq[\"Authorization\"] = \"Bearer #{ENV['INFRAI_API_KEY']}\"\nreq[\"Content-Type\"] = \"application/json\"\nreq.body = '{\"messages\": [{\"role\": \"system\"}]}'\nres = http.request(req)\nputs res.body","rust":"// Zero-install REST call — no SDK required (short-term the API is REST-only). (uses the reqwest + tokio crates)\nuse std::env;\n\n#[tokio::main]\nasync fn main() -> Result<(), Box<dyn std::error::Error>> {\n    let resp = reqwest::Client::new()\n        .post(\"https://api.infrai.cc/v1/ai/tokens/count\")\n        .header(\"Authorization\", format!(\"Bearer {}\", env::var(\"INFRAI_API_KEY\")?))\n        .header(\"Content-Type\", \"application/json\")\n        .body(r#\"{\"messages\": [{\"role\": \"system\"}]}\"#)\n        .send()\n        .await?;\n    println!(\"{}\", resp.text().await?);\n    Ok(())\n}","request":{"messages":[{"role":"system"}]}},"billing":{"is_billable":false,"free":true,"billing_class":"free","unit":"per_call","note":"free (rate-limited); does NOT consume the new-account trial"}}