### Install Dependencies Source: https://kilo.ai/docs/gateway/quickstart Install the necessary npm packages for your project. ```bash npm install ai @ai-sdk/openai dotenv ``` -------------------------------- ### Go SDK Example Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Example of how to use the Kilo AI Gateway API with Go, making a POST request to the chat completions endpoint. ```Go package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { body := map[string]interface{}{ "model": "anthropic/claude-sonnet-4.5", "messages": []map[string]string{ {"role": "user", "content": "Why is the sky blue?"}, }, } jsonBody, _ := json.Marshal(body) req, _ := http.NewRequest("POST", "https://api.kilo.ai/api/gateway/chat/completions", bytes.NewBuffer(jsonBody)) req.Header.Set("Authorization", "Bearer "+os.Getenv("KILO_API_KEY")) req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } defer resp.Body.Close() respBody, _ := io.ReadAll(resp.Body) fmt.Println(string(respBody)) } ``` -------------------------------- ### Ruby SDK Example Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Example of how to use the Kilo AI Gateway API with Ruby, making a POST request to the chat completions endpoint. ```Ruby require 'net/http' require 'json' uri = URI('https://api.kilo.ai/api/gateway/chat/completions') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri) request['Authorization'] = "Bearer #{ENV['KILO_API_KEY']}" request['Content-Type'] = 'application/json' request.body = { model: 'anthropic/claude-sonnet-4.5', messages: [ { role: 'user', content: 'Why is the sky blue?' } ] }.to_json response = http.request(request) result = JSON.parse(response.body) puts result['choices'][0]['message']['content'] ``` -------------------------------- ### Install OpenAI SDK (npm) Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Install the OpenAI Node.js package using npm. ```bash npm install openai ``` -------------------------------- ### Install OpenAI SDK (pip) Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Install the OpenAI Python package using pip. ```bash pip install openai ``` -------------------------------- ### Install Vercel AI SDK and OpenAI integration Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Install the necessary packages for using the Vercel AI SDK with OpenAI compatibility. This is the first step to integrate with the Kilo Gateway. ```bash npm install ai @ai-sdk/openai ``` -------------------------------- ### Install Pi Provider Extension Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Install the Kilo-maintained Pi provider extension for the Pi coding agent. This enables access to Kilo Gateway models. ```bash pi install git:github.com/Kilo-Org/kilo-pi-provider ``` -------------------------------- ### List Models Response Example Source: https://kilo.ai/docs/gateway/api-reference An example response from the List Models endpoint, showing model details including ID, name, and pricing. This response is OpenAI-compatible. ```json { "data": [ { "id": "anthropic/claude-sonnet-4.5", "object": "model", "created": 1739000000, "owned_by": "anthropic", "name": "Claude Sonnet 4.5", "context_length": 200000, "pricing": { "prompt": "0.000003", "completion": "0.000015" } } ] } ``` -------------------------------- ### Chat Completions - Streaming Source: https://kilo.ai/docs/gateway/sdks-and-frameworks This example shows how to enable and handle streaming responses from the chat completions endpoint, receiving tokens as they are generated. ```APIDOC ## POST /api/gateway/chat/completions (Streaming) ### Description Sends a chat message to the gateway and receives completions in a streaming fashion, allowing for real-time token delivery. ### Method POST ### Endpoint https://api.kilo.ai/api/gateway/chat/completions ### Parameters #### Query Parameters - **stream** (boolean) - Optional - Set to `true` to enable streaming responses. ### Request Body - **model** (string) - Required - The model to use for completion (e.g., "anthropic/claude-sonnet-4.5"). - **messages** (array) - Required - An array of message objects, each with a 'role' (user, assistant, system) and 'content'. - **stream** (boolean) - Required - Set to `true` to enable streaming. ### Request Example ```json { "model": "anthropic/claude-sonnet-4.5", "messages": [ {"role": "user", "content": "Write a short story about AI."} ], "stream": true } ``` ### Response #### Success Response (200) - The response will be a stream of JSON objects, each representing a chunk of the completion. - **choices** (array) - **delta** (object) - **content** (string) - The text content of the current token. #### Response Example (Streaming Chunks) ```json {"id": "chatcmpl-123", "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": null}]} {"id": "chatcmpl-123", "choices": [{"index": 0, "delta": {"content": "Once"}, "finish_reason": null}]} {"id": "chatcmpl-123", "choices": [{"index": 0, "delta": {"content": " upon"}, "finish_reason": null}]} {"id": "chatcmpl-123", "choices": [{"index": 0, "delta": {"content": " a"}, "finish_reason": null}]} {"id": "chatcmpl-123", "choices": [{"index": 0, "delta": {"content": " time"}, "finish_reason": null}]} {"id": "chatcmpl-123", "choices": [{"index": 0, "delta": {"content": "..."}, "finish_reason": "stop"}]} ``` ``` -------------------------------- ### Example FIM Completions Request Source: https://kilo.ai/docs/gateway/api-reference An example of how to call the FIM completions endpoint using curl. Includes model, prompt, suffix, and stream settings. ```bash curl -X POST "https://api.kilo.ai/api/fim/completions" \ -H "Authorization: Bearer $KILO_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "mistralai/codestral-2508", "prompt": "def fibonacci(n):\n if n <= 1:\n return n\n ", "suffix": "\n\nprint(fibonacci(10))", "max_tokens": 200, "stream": false }' ``` -------------------------------- ### Chat Completions - Non-streaming Source: https://kilo.ai/docs/gateway/sdks-and-frameworks This example demonstrates how to make a non-streaming POST request to the chat completions endpoint to get a single response. ```APIDOC ## POST /api/gateway/chat/completions ### Description Sends a chat message to the gateway and receives a single completion response. ### Method POST ### Endpoint https://api.kilo.ai/api/gateway/chat/completions ### Request Body - **model** (string) - Required - The model to use for completion (e.g., "anthropic/claude-sonnet-4.5"). - **messages** (array) - Required - An array of message objects, each with a 'role' (user, assistant, system) and 'content'. ### Request Example ```json { "model": "anthropic/claude-sonnet-4.5", "messages": [ {"role": "user", "content": "What is the capital of France?"} ] } ``` ### Response #### Success Response (200) - **choices** (array) - Contains the completion result. - **message** (object) - **content** (string) - The generated text response. #### Response Example ```json { "id": "chatcmpl-123", "object": "chat.completion", "created": 1700000000, "model": "anthropic/claude-sonnet-4.5", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The capital of France is Paris." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15 } } ``` ``` -------------------------------- ### Raw SSE Format Example Source: https://kilo.ai/docs/gateway/streaming The Kilo AI Gateway returns streaming data in Server-Sent Events (SSE) format. Each event is a JSON object prefixed with `data: `. ```text data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1234567890,"model":"anthropic/claude-sonnet-4.5","choices":[{"index":0,"delta":{"role":"assistant","content":"Once"},"finish_reason":null}]} data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1234567890,"model":"anthropic/claude-sonnet-4.5","choices":[{"index":0,"delta":{"content":" upon"},"finish_reason":null}]} data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1234567890,"model":"anthropic/claude-sonnet-4.5","choices":[{"index":0,"delta":{"content":" a"},"finish_reason":null}]} data: [DONE] ``` -------------------------------- ### Streaming text with tool calling using Vercel AI SDK Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Implement AI text generation with tool calling capabilities using the Vercel AI SDK and Kilo Gateway. This example defines a `getWeather` tool for retrieving weather information. ```typescript import { streamText, tool } from "ai" import { createOpenAI } from "@ai-sdk/openai" import { z } from "zod" const kilo = createOpenAI({ baseURL: "https://api.kilo.ai/api/gateway", apiKey: process.env.KILO_API_KEY, }) const result = streamText({ model: kilo.chat("anthropic/claude-sonnet-4.5"), prompt: "What is the weather in San Francisco?", tools: { getWeather: tool({ description: "Get the current weather for a location", parameters: z.object({ location: z.string().describe("City name"), }), execute: async ({ location }) => { return { temperature: 72, condition: "sunny" } }, }), }, }) for await (const textPart of result.textStream) { process.stdout.write(textPart) } ``` -------------------------------- ### Chat Completions with OpenAI SDK Source: https://kilo.ai/docs/gateway/quickstart Use the OpenAI SDK to interact with the Kilo AI Gateway by setting the correct baseURL and providing your API key. This example demonstrates fetching a chat completion. ```typescript import OpenAI from "openai" const client = new OpenAI({ apiKey: process.env.KILO_API_KEY, baseURL: "https://api.kilo.ai/api/gateway", }) const response = await client.chat.completions.create({ model: "anthropic/claude-sonnet-4.5", messages: [{ role: "user", content: "Why is the sky blue?" }], }) console.log(response.choices[0].message.content) ``` -------------------------------- ### Example cURL Request for Chat Completions Source: https://kilo.ai/docs/gateway/api-reference Demonstrates how to make a POST request to the chat completions endpoint using cURL. Includes setting the API key and providing a JSON payload. ```bash curl -X POST "https://api.kilo.ai/api/gateway/chat/completions" \ -H "Authorization: Bearer $KILO_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "anthropic/claude-sonnet-4.5", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is quantum computing?"} ], "max_tokens": 500, "temperature": 0.7 }' ``` -------------------------------- ### Streaming with Vercel AI SDK Source: https://kilo.ai/docs/gateway/streaming Use the Vercel AI SDK for a simplified interface to handle SSE parsing and streaming. Ensure you have the SDK installed and your Kilo API key configured. ```typescript import { streamText } from "ai" import { createOpenAI } from "@ai-sdk/openai" const kilo = createOpenAI({ baseURL: "https://api.kilo.ai/api/gateway", apiKey: process.env.KILO_API_KEY, }) const result = streamText({ model: kilo.chat("anthropic/claude-sonnet-4.5"), prompt: "Write a short story about a robot.", }) for await (const textPart of result.textStream) { process.stdout.write(textPart) } // Access usage data after streaming completes const usage = await result.usage console.log("Tokens used:", usage) ``` -------------------------------- ### Model Allow List Examples Source: https://kilo.ai/docs/gateway/usage-and-billing Organizations can restrict model usage by defining an allow list. This list supports exact model names and wildcard patterns for broader restrictions. ```text # Examples of allow list entries anthropic/claude-sonnet-4.5 # Specific model anthropic/* # All Anthropic models openai/gpt-5.2 # Specific OpenAI model ``` -------------------------------- ### Example Chat Completion Response Source: https://kilo.ai/docs/gateway/api-reference Illustrates the structure of a typical non-streaming response from the chat completions endpoint, including generated content and usage tokens. ```json { "id": "gen-abc123", "object": "chat.completion", "created": 1739000000, "model": "anthropic/claude-sonnet-4.5", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Quantum computing is a type of computation that uses quantum mechanics..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 25, "completion_tokens": 150, "total_tokens": 175 } } ``` -------------------------------- ### Chat Completions Request with Mode Header Source: https://kilo.ai/docs/gateway/models-and-providers This example demonstrates how to make a chat completions request using `curl`, specifying an auto model and a specific mode via the `x-kilocode-mode` header. Ensure you replace `$KILO_API_KEY` with your actual API key. ```Shell curl -X POST "https://api.kilo.ai/api/gateway/chat/completions" \ -H "Authorization: Bearer $KILO_API_KEY" \ -H "x-kilocode-mode: plan" \ -H "Content-Type: application/json" \ -d '{"model": "kilo-auto/balanced", "messages": [{"role": "user", "content": "Design a database schema"}]}' ``` -------------------------------- ### FIM Completions API Endpoint Source: https://kilo.ai/docs/gateway/api-reference Use this endpoint for fill-in-the-middle code generation tasks. Ensure the model ID starts with 'mistralai/'. ```http POST /api/fim/completions ``` -------------------------------- ### Chat Completions Request with Auto Model Source: https://kilo.ai/docs/gateway/models-and-providers Example of a chat completions request using an auto model tier. The 'model' field specifies the auto model to use, and 'messages' contains the conversation history. ```JSON { "model": "kilo-auto/frontier", "messages": [{ "role": "user", "content": "Help me design a database schema" }] } ``` -------------------------------- ### Create Project Directory Source: https://kilo.ai/docs/gateway/quickstart Use these commands to create a new project directory and navigate into it. ```bash mkdir my-ai-app cd my-ai-app npm init -y ``` -------------------------------- ### Configure and Use OpenAI SDK (Python) Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Configure the OpenAI client with the Kilo Gateway's base URL and API key for non-streaming and streaming chat completions. ```python import os from openai import OpenAI client = OpenAI( api_key=os.getenv("KILO_API_KEY"), base_url="https://api.kilo.ai/api/gateway", ) # Non-streaming response = client.chat.completions.create( model="anthropic/claude-sonnet-4.5", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain quantum entanglement simply."}, ], ) print(response.choices[0].message.content) # Streaming stream = client.chat.completions.create( model="anthropic/claude-sonnet-4.5", messages=[ {"role": "user", "content": "Write a poem about the ocean."}, ], stream=True, ) for chunk in stream: content = chunk.choices[0].delta.content if content: print(content, end="", flush=True) ``` -------------------------------- ### Basic streaming text with Vercel AI SDK Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Demonstrates basic AI text generation using the Vercel AI SDK and the Kilo Gateway. Ensure your KILO_API_KEY is set in your environment variables. ```typescript import { streamText } from "ai" import { createOpenAI } from "@ai-sdk/openai" const kilo = createOpenAI({ baseURL: "https://api.kilo.ai/api/gateway", apiKey: process.env.KILO_API_KEY, }) const result = streamText({ model: kilo.chat("anthropic/claude-sonnet-4.5"), prompt: "Write a haiku about programming.", }) for await (const textPart of result.textStream) { process.stdout.write(textPart) } ``` -------------------------------- ### Configure and Use OpenAI SDK (TypeScript/JavaScript) Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Configure the OpenAI client with the Kilo Gateway's base URL and API key for non-streaming and streaming chat completions. ```typescript import OpenAI from "openai" const client = new OpenAI({ apiKey: process.env.KILO_API_KEY, baseURL: "https://api.kilo.ai/api/gateway", }) // Non-streaming const response = await client.chat.completions.create({ model: "anthropic/claude-sonnet-4.5", messages: [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: "Explain quantum entanglement simply." }, ], }) console.log(response.choices[0].message.content) // Streaming const stream = await client.chat.completions.create({ model: "anthropic/claude-sonnet-4.5", messages: [{ role: "user", content: "Write a poem about the ocean." }], stream: true, }) for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content if (content) process.stdout.write(content) } ``` -------------------------------- ### List Available Models Source: https://kilo.ai/docs/gateway/models-and-providers Use this endpoint to retrieve information about all available models, including their pricing, context window, and supported features. No authentication is required. ```HTTP GET https://api.kilo.ai/api/gateway/models ``` -------------------------------- ### Initialize Kilo OpenAI Client with API Key Source: https://kilo.ai/docs/gateway/authentication Configure the Kilo OpenAI client with your base URL and API key. Ensure your Kilo API key is set as an environment variable. ```typescript import { createOpenAI } from "@ai-sdk/openai" const kilo = createOpenAI({ baseURL: "https://api.kilo.ai/api/gateway", apiKey: process.env.KILO_API_KEY, }) ``` -------------------------------- ### Run Script Source: https://kilo.ai/docs/gateway/quickstart Execute your JavaScript script using Node.js. ```bash node index.mjs ``` -------------------------------- ### Set Kilo API Key Source: https://kilo.ai/docs/gateway/quickstart Create a .env file to store your Kilo API key. Ensure you have API credits in your Kilo account. ```dotenv KILO_API_KEY=your_api_key_here ``` -------------------------------- ### Streaming with OpenAI SDK (TypeScript) Source: https://kilo.ai/docs/gateway/streaming Utilize the OpenAI SDK for streaming chat completions. Configure the SDK with your Kilo API key and the gateway's base URL. ```typescript import OpenAI from "openai" const client = new OpenAI({ apiKey: process.env.KILO_API_KEY, baseURL: "https://api.kilo.ai/api/gateway", }) const stream = await client.chat.completions.create({ model: "anthropic/claude-sonnet-4.5", messages: [{ role: "user", content: "Write a short story" }], stream: true, }) for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content if (content) { process.stdout.write(content) } } ``` -------------------------------- ### Stream Text with Vercel AI SDK Source: https://kilo.ai/docs/gateway/quickstart This script uses the Vercel AI SDK to stream text from a model via the Kilo AI Gateway. It requires the 'ai', '@ai-sdk/openai', and 'dotenv' packages. ```javascript import { streamText } from "ai" import { createOpenAI } from "@ai-sdk/openai" import "dotenv/config" const kilo = createOpenAI({ baseURL: "https://api.kilo.ai/api/gateway", apiKey: process.env.KILO_API_KEY, }) async function main() { const result = streamText({ model: kilo.chat("anthropic/claude-sonnet-4.5"), prompt: "Invent a new holiday and describe its traditions.", }) for await (const textPart of result.textStream) { process.stdout.write(textPart) } console.log() console.log("Token usage:", await result.usage) console.log("Finish reason:", await result.finishReason) } main().catch(console.error) ``` -------------------------------- ### List Available Models Source: https://kilo.ai/docs/gateway/models-and-providers Retrieve a comprehensive list of all available AI models, including their pricing, context window size, and supported features. No authentication is required for this endpoint. ```APIDOC ## GET /api/gateway/models ### Description Retrieves a list of all available models with their details. ### Method GET ### Endpoint https://api.kilo.ai/api/gateway/models ### Parameters None ### Request Example None ### Response #### Success Response (200) - **models** (array) - List of available models with their properties. ### Response Example ```json [ { "id": "anthropic/claude-opus-4.7", "provider": "Anthropic", "description": "Most capable Claude model for complex reasoning" }, { "id": "openai/gpt-5.4", "provider": "OpenAI", "description": "Latest GPT model" } ] ``` ``` -------------------------------- ### List models Source: https://kilo.ai/docs/gateway/api-reference Retrieves a list of available models compatible with the OpenAI API format. Includes model ID, object type, creation time, owner, name, context length, and pricing. ```APIDOC ## GET /models ### Description Retrieve the list of available models. Returns an OpenAI-compatible model list. ### Method GET ### Endpoint /models ### Parameters No parameters required. ### Request Example (No example provided in source) ### Response #### Success Response (200) - **data** (array) - List of model objects. - **id** (string) - Model identifier. - **object** (string) - Type of object, e.g., "model". - **created** (integer) - Timestamp of model creation. - **owned_by** (string) - The organization that owns the model. - **name** (string) - Human-readable name of the model. - **context_length** (integer) - Maximum context length the model supports. - **pricing** (object) - Pricing information for the model. - **prompt** (string) - Cost per prompt token. - **completion** (string) - Cost per completion token. #### Response Example ```json { "data": [ { "id": "anthropic/claude-sonnet-4.5", "object": "model", "created": 1739000000, "owned_by": "anthropic", "name": "Claude Sonnet 4.5", "context_length": 200000, "pricing": { "prompt": "0.000003", "completion": "0.000015" } } ] } ``` ``` -------------------------------- ### Send a chat completion request with Go Source: https://kilo.ai/docs/gateway/sdks-and-frameworks This Go program demonstrates how to send a chat completion request to the Kilo AI Gateway. It marshals a JSON payload and sends an HTTP POST request. Ensure your API key is set as an environment variable. ```go package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { body := map[string]interface{}{ "model": "anthropic/claude-sonnet-4.5", "messages": []map[string]string{ {"role": "user", "content": "Why is the sky blue?"}, }, } jsonBody, _ := json.Marshal(body) req, _ := http.NewRequest("POST", "https://api.kilo.ai/api/gateway/chat/completions", bytes.NewBuffer(jsonBody)) req.Header.Set("Authorization", "Bearer "+os.Getenv("KILO_API_KEY")) req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } defer resp.Body.Close() respBody, _ := io.ReadAll(resp.Body) fmt.Println(string(respBody)) } ``` -------------------------------- ### Chat Completions with cURL Source: https://kilo.ai/docs/gateway/quickstart Make a POST request to the Kilo AI Gateway's chat completions endpoint using cURL. Ensure your KILO_API_KEY environment variable is set. ```bash curl -X POST "https://api.kilo.ai/api/gateway/chat/completions" \ -H "Authorization: Bearer $KILO_API_KEY" \ -H "Content-Type: application/json" \ -d '{ \ "model": "anthropic/claude-sonnet-4.5", \ "messages": [ \ { \ "role": "user", \ "content": "Why is the sky blue?" \ } \ ], \ "stream": false \ }' ``` -------------------------------- ### Next.js API route for AI streaming with Vercel AI SDK Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Set up a Next.js API route to handle AI streaming requests using the Vercel AI SDK and Kilo Gateway. This allows clients to receive streaming responses directly. ```typescript import { streamText } from "ai" import { createOpenAI } from "@ai-sdk/openai" const kilo = createOpenAI({ baseURL: "https://api.kilo.ai/api/gateway", apiKey: process.env.KILO_API_KEY, }) export async function POST(request: Request) { const { messages } = await request.json() const result = streamText({ model: kilo.chat("anthropic/claude-sonnet-4.5"), messages, }) return result.toDataStreamResponse() } ``` -------------------------------- ### Final SSE Chunk with Usage Data Source: https://kilo.ai/docs/gateway/streaming The final SSE chunk before `[DONE]` includes token usage information and an empty `choices` array. ```json { "id": "chatcmpl-abc123", "object": "chat.completion.chunk", "usage": { "prompt_tokens": 12, "completion_tokens": 150, "total_tokens": 162 }, "choices": [] } ``` -------------------------------- ### Request with Tools for Function Calling Source: https://kilo.ai/docs/gateway/api-reference Send a request to the gateway specifying the model, messages, available tools, and tool choice strategy. This is used when you want the AI to be able to call external functions. ```json { "model": "anthropic/claude-sonnet-4.5", "messages": [{ "role": "user", "content": "What's the weather in San Francisco?" }], "tools": [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City name" } }, "required": ["location"] } } } ], "tool_choice": "auto" } ``` -------------------------------- ### LangChain Integration with Kilo AI Gateway Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Configure LangChain's ChatOpenAI to use the Kilo AI Gateway. Ensure the KILO_API_KEY environment variable is set. ```python from langchain_openai import ChatOpenAI llm = ChatOpenAI( model="anthropic/claude-sonnet-4.5", api_key=os.getenv("KILO_API_KEY"), base_url="https://api.kilo.ai/api/gateway", ) response = llm.invoke("Explain photosynthesis in simple terms.") print(response.content) ``` -------------------------------- ### Send a chat completion request with Ruby Source: https://kilo.ai/docs/gateway/sdks-and-frameworks This Ruby script shows how to make a POST request to the Kilo AI Gateway for chat completions. It sets necessary headers and sends a JSON payload. Ensure your API key is set as an environment variable. ```ruby require 'net/http' require 'json' uri = URI('https://api.kilo.ai/api/gateway/chat/completions') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri) request['Authorization'] = "Bearer #{ENV['KILO_API_KEY']}" request['Content-Type'] = 'application/json' request.body = { model: 'anthropic/claude-sonnet-4.5', messages: [ { role: 'user', content: 'Why is the sky blue?' } ] }.to_json response = http.request(request) result = JSON.parse(response.body) puts result['choices'][0]['message']['content'] ``` -------------------------------- ### Tool Call Response from Assistant Source: https://kilo.ai/docs/gateway/api-reference The assistant's response when it decides to call a tool. It includes the message role, content (null if tool calls are present), and details of the tool calls made. ```json { "choices": [ { "message": { "role": "assistant", "content": null, "tool_calls": [ { "id": "call_abc123", "type": "function", "function": { "name": "get_weather", "arguments": "{\"location\":\"San Francisco\"}" } } ] }, "finish_reason": "tool_calls" } ] } ``` -------------------------------- ### Stream Text with Kilo AI Gateway Source: https://kilo.ai/docs/gateway This snippet demonstrates how to use the Kilo AI Gateway with the Vercel AI SDK to stream text responses from a specific model. Ensure your KILO_API_KEY is set in your environment variables. ```typescript import { streamText } from "ai" import { createOpenAI } from "@ai-sdk/openai" const kilo = createOpenAI({ baseURL: "https://api.kilo.ai/api/gateway", apiKey: process.env.KILO_API_KEY, }) const result = streamText({ model: kilo.chat("anthropic/claude-sonnet-4.5"), prompt: "Why is the sky blue?", }) ``` -------------------------------- ### Chat Completions Source: https://kilo.ai/docs/gateway/api-reference Create a chat completion. This is the primary endpoint for interacting with AI models. It supports both standard and streaming responses. ```APIDOC ## POST /chat/completions ### Description Creates a chat completion by interacting with AI models. This endpoint is the main interface for AI model interactions and supports streaming responses via Server-Sent Events (SSE). ### Method POST ### Endpoint https://api.kilo.ai/api/gateway/chat/completions ### Parameters #### Request Body - **model** (string) - Required - Model ID (e.g., "anthropic/claude-sonnet-4.5") - **messages** (Message[]) - Required - Array of conversation messages - **stream** (boolean) - Optional - Enable SSE streaming (default: false) - **max_tokens** (number) - Optional - Maximum tokens to generate - **temperature** (number) - Optional - Sampling temperature (0-2) - **top_p** (number) - Optional - Nucleus sampling (0-1) - **stop** (string | string[]) - Optional - Stop sequences - **frequency_penalty** (number) - Optional - Frequency penalty (-2 to 2) - **presence_penalty** (number) - Optional - Presence penalty (-2 to 2) - **tools** (Tool[]) - Optional - Available tools/functions - **tool_choice** (ToolChoice) - Optional - Tool selection strategy - **response_format** (ResponseFormat) - Optional - Structured output configuration - **user** (string) - Optional - End-user identifier for safety - **seed** (number) - Optional - Deterministic sampling seed ### Request Example ```json { "model": "anthropic/claude-sonnet-4.5", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is quantum computing?"} ], "max_tokens": 500, "temperature": 0.7 } ``` ### Response (non-streaming) #### Success Response (200) - **id** (string) - Unique identifier for the completion - **object** (string) - Type of object, "chat.completion" - **created** (number) - Timestamp of creation - **model** (string) - The model used for completion - **choices** (Array) - Array of completion choices - **index** (number) - Index of the choice - **message** (object) - The generated message - **role** (string) - Role of the message sender ("assistant") - **content** (string | null) - The content of the message - **tool_calls** (ToolCall[]) - Tool calls if any - **finish_reason** (string) - Reason for finishing generation ("stop", "length", "tool_calls", "content_filter") - **usage** (object) - Token usage statistics - **prompt_tokens** (number) - Tokens in the prompt - **completion_tokens** (number) - Tokens in the completion - **total_tokens** (number) - Total tokens used ### Response Example (non-streaming) ```json { "id": "gen-abc123", "object": "chat.completion", "created": 1739000000, "model": "anthropic/claude-sonnet-4.5", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Quantum computing is a type of computation that uses quantum mechanics..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 25, "completion_tokens": 150, "total_tokens": 175 } } ``` ### Response (streaming) When `stream: true`, the response is a series of SSE events. #### Success Response (200) - **id** (string) - Unique identifier for the completion chunk - **object** (string) - Type of object, "chat.completion.chunk" - **created** (number) - Timestamp of creation - **model** (string) - The model used for completion - **choices** (Array) - Array of completion choices - **index** (number) - Index of the choice - **delta** (object) - The change in the message content - **role** (string) - Role of the message sender ("assistant") - **content** (string) - The content of the message chunk - **tool_calls** (ToolCall[]) - Tool calls if any - **finish_reason** (string | null) - Reason for finishing generation - **usage** (object) - Token usage statistics (Only in the final chunk) - **prompt_tokens** (number) - **completion_tokens** (number) - **total_tokens** (number) ### Response Example (streaming) ``` data: {"id": "gen-abc123", "object": "chat.completion.chunk", "created": 1739000000, "model": "anthropic/claude-sonnet-4.5", "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": null}]} data: {"id": "gen-abc123", "object": "chat.completion.chunk", "created": 1739000000, "model": "anthropic/claude-sonnet-4.5", "choices": [{"index": 0, "delta": {"content": "Quantum computing is a type"}, "finish_reason": null}]} data: {"id": "gen-abc123", "object": "chat.completion.chunk", "created": 1739000000, "model": "anthropic/claude-sonnet-4.5", "choices": [{"index": 0, "delta": {"content": " of computation that uses"}, "finish_reason": null}]} ... data: {"id": "gen-abc123", "object": "chat.completion.chunk", "created": 1739000000, "model": "anthropic/claude-sonnet-4.5", "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 25, "completion_tokens": 150, "total_tokens": 175}} ``` -------------------------------- ### LangChain.js Integration with Kilo AI Gateway Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Configure LangChain.js ChatOpenAI to use the Kilo AI Gateway. Ensure the KILO_API_KEY environment variable is set. ```javascript import { ChatOpenAI } from "@langchain/openai" const model = new ChatOpenAI({ modelName: "anthropic/claude-sonnet-4.5", openAIApiKey: process.env.KILO_API_KEY, configuration: { baseURL: "https://api.kilo.ai/api/gateway", }, }) const response = await model.invoke("Explain photosynthesis in simple terms.") console.log(response.content) ``` -------------------------------- ### Enable Streaming in Request Body Source: https://kilo.ai/docs/gateway/streaming Set `stream: true` in your request JSON to enable streaming responses from the Kilo AI Gateway. ```json { "model": "anthropic/claude-sonnet-4.5", "messages": [{ "role": "user", "content": "Write a short story" }], "stream": true } ``` -------------------------------- ### List providers Source: https://kilo.ai/docs/gateway/api-reference Retrieves a list of available service providers. No authentication is required. ```APIDOC ## GET /providers ### Description Retrieve the list of available providers. ### Method GET ### Endpoint /providers ### Parameters No parameters required. ### Request Example (No example provided in source) ### Response #### Success Response (200) (Response structure not explicitly defined in source, but implies a list of providers) #### Response Example (Example response not provided in source) ``` -------------------------------- ### Send a streaming chat completion request with cURL Source: https://kilo.ai/docs/gateway/sdks-and-frameworks Use this command to send a streaming request to the chat completions endpoint. The `-N` flag ensures you receive tokens as they are generated. Ensure your API key is set as an environment variable. ```bash curl -N -X POST "https://api.kilo.ai/api/gateway/chat/completions" \ -H "Authorization: Bearer $KILO_API_KEY" \ -H "Content-Type: application/json" \ -d '{ \ "model": "anthropic/claude-sonnet-4.5", \ "messages": [ \ {"role": "user", "content": "Write a short story about AI."} \ ], \ "stream": true \ }' ``` -------------------------------- ### Insufficient Balance Error Response Source: https://kilo.ai/docs/gateway/usage-and-billing When your account balance reaches zero, paid model requests will return an HTTP 402 error with details on how to add credits. ```json { "error": { "message": "Insufficient balance. Please add credits to continue.", "code": 402, "metadata": { "buyCreditsUrl": "https://app.kilo.ai/credits" } } } ``` -------------------------------- ### List Models API Endpoint Source: https://kilo.ai/docs/gateway/api-reference Use this endpoint to retrieve a list of all available models. No authentication is required. ```http GET /models ``` -------------------------------- ### Specify Model in SDK Request Source: https://kilo.ai/docs/gateway/models-and-providers Use this snippet to specify a model like Anthropic's Claude Sonnet 4.6 within an SDK request. Ensure the `kilo.chat` function is correctly imported and configured. ```javascript const result = streamText({ model: kilo.chat("anthropic/claude-sonnet-4.6"), prompt: "Hello!", }) ``` -------------------------------- ### Chat Completions Endpoint Source: https://kilo.ai/docs/gateway/api-reference This is the primary endpoint for interacting with AI models via chat completions. Use POST requests to this path. ```http POST /chat/completions ``` -------------------------------- ### FIM completions Source: https://kilo.ai/docs/gateway/api-reference Generates fill-in-the-middle completions for code generation using Mistral Codestral models. Supports specifying model, prompt, suffix, and generation parameters. ```APIDOC ## POST /api/fim/completions ### Description Generates fill-in-the-middle completions for code generation, powered by Mistral Codestral. ### Method POST ### Endpoint /api/fim/completions ### Parameters #### Request Body - **model** (string) - Required - Must be a Mistral model (e.g., "mistralai/codestral-2508") - **prompt** (string) - Required - Code before the cursor - **suffix** (string) - Optional - Code after the cursor - **max_tokens** (number) - Optional - Maximum tokens (capped at 1000) - **temperature** (number) - Optional - **stop** (string[]) - Optional - **stream** (boolean) - Optional ### Request Example ```json { "model": "mistralai/codestral-2508", "prompt": "def fibonacci(n):\n if n <= 1:\n return n\n ", "suffix": "\n\nprint(fibonacci(10))", "max_tokens": 200, "stream": false } ``` ### Response #### Success Response (200) (Response structure not explicitly defined in source, but implies completion data) #### Response Example (Example response not provided in source) ``` -------------------------------- ### Kilo AI Gateway Base URL Source: https://kilo.ai/docs/gateway The base URL for all Kilo AI Gateway API requests. ```bash https://api.kilo.ai/api/gateway ``` -------------------------------- ### FIM Completions Request Body Schema Source: https://kilo.ai/docs/gateway/api-reference Defines the structure for FIM completion requests. The 'prompt' is the code before the cursor, and 'suffix' is the code after. ```typescript type FIMRequest = { model: string // Must be a Mistral model (e.g., "mistralai/codestral-2508") prompt: string // Code before the cursor suffix?: string // Code after the cursor max_tokens?: number // Maximum tokens (capped at 1000) temperature?: number stop?: string[] stream?: boolean } ``` -------------------------------- ### API Key Authentication Header Source: https://kilo.ai/docs/gateway/authentication Use this header for primary authentication with your API key. The API key is a JWT token. ```text Authorization: Bearer ``` -------------------------------- ### Specify Model in Raw API Request Source: https://kilo.ai/docs/gateway/models-and-providers This JSON structure shows how to specify a model for a raw API request. The `model` field should be set to the desired `provider/model-name`. ```json { "model": "anthropic/claude-sonnet-4.6", "messages": [{ "role": "user", "content": "Hello!" }] } ``` -------------------------------- ### Chat Completion Response (Streaming) Source: https://kilo.ai/docs/gateway/api-reference Defines the structure of the Server-Sent Events (SSE) chunks when streaming is enabled. Contains partial completion data. ```typescript type ChatCompletionChunk = { id: string object: "chat.completion.chunk" created: number model: string choices: Array<{ index: number delta: { role?: "assistant" content?: string tool_calls?: ToolCall[] } finish_reason: string | null }> // Only in the final chunk usage?: { prompt_tokens: number completion_tokens: number total_tokens: number } } ``` -------------------------------- ### List Providers API Endpoint Source: https://kilo.ai/docs/gateway/api-reference Use this endpoint to retrieve a list of all available providers. No authentication is required. ```http GET /providers ``` -------------------------------- ### Organization Token Header Source: https://kilo.ai/docs/gateway/authentication Include this header when making requests on behalf of an organization. Organization tokens have a 15-minute expiry. ```text X-KiloCode-OrganizationId: your_org_id ```