Source: https://ailoot.ai/docs/en/connect/openai-sdk/
Canonical: https://ailoot.ai/docs/en/connect/openai-sdk/
Language: en

[Connection](https://ailoot.ai/docs/en/) 

# OpenAI SDK

[Markdown](https://ailoot.ai/docs/en/connect/openai-sdk/index.md)

The official OpenAI libraries for Python and Node.js work with Ailoot without changes to your code: only the URL and key change.

## [](https://ailoot.ai/docs/en/connect/openai-sdk/#install)Installation

```bash
pip install openai
```

```bash
npm install openai
```

## [](https://ailoot.ai/docs/en/connect/openai-sdk/#client)Connection

Set the base URL to `https://api.ailoot.ai/v1`. You can set it in code or through the variables `OPENAI_BASE_URL` and `OPENAI_API_KEY` — then your code needs no changes at all.

```python
from openai import OpenAI
import os

client = OpenAI(max_retries=0, 
    base_url="https://api.ailoot.ai/v1",
    api_key=os.environ["AILOOT_API_KEY"],
)

resp = client.chat.completions.create(
    model="claude-sonnet-5-5",
    messages=[{"role": "user", "content": "Explain what an API is in two sentences."}],
)
print(resp.choices[0].message.content)
```

```js
import OpenAI from "openai";

const client = new OpenAI({ maxRetries: 0,
  baseURL: "https://api.ailoot.ai/v1",
  apiKey: process.env.AILOOT_API_KEY,
});

const resp = await client.chat.completions.create({
  model: "claude-sonnet-5-5",
  messages: [{ role: "user", content: "Explain what an API is in two sentences." }],
});
console.log(resp.choices[0].message.content);
```

## [](https://ailoot.ai/docs/en/connect/openai-sdk/#responses)Responses API

The newer OpenAI format. It is available for models whose catalog entry lists Responses: 53 identifiers, including Claude, GPT, DeepSeek, Qwen, Kimi, and GLM.

```python
resp = client.responses.create(
    model="gpt-6.1-sol",
    instructions="Keep your answers brief and to the point.",
    input="How does TCP differ from UDP?",
)
print(resp.output_text)
```

```js
const resp = await client.responses.create({
  model: "gpt-6.1-sol",
  instructions: "Keep your answers brief and to the point.",
  input: "How does TCP differ from UDP?",
});
console.log(resp.output_text);
```

## [](https://ailoot.ai/docs/en/connect/openai-sdk/#tools)Tools (function calling)

```python
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Weather in a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

import json

messages = [{"role": "user", "content": "What is the weather like in Kazan?"}]
# Disable automatic retries: after a timeout, check the Request ID in your usage history first.
client = client.with_options(max_retries=0)
for turn in range(4):
    resp = client.chat.completions.create(
        model="claude-sonnet-5-5", messages=messages, tools=tools,
    )
    message = resp.choices[0].message
    messages.append(message.model_dump(exclude_none=True))
    if not message.tool_calls:
        print(message.content)
        break
    for call in message.tool_calls:
        if call.function.name != "get_weather":
            raise ValueError("Unexpected function")
        arguments = json.loads(call.function.arguments)
        # Replace with your own weather function; this example is explicitly demo data.
        result = {"city": arguments["city"], "temperature_c": 20, "demo": True}
        messages.append({"role": "tool", "tool_call_id": call.id,
                         "content": json.dumps(result, ensure_ascii=False)})
else:
    raise RuntimeError("Tool loop limit reached")
```

## [](https://ailoot.ai/docs/en/connect/openai-sdk/#embeddings)Embeddings

```python
emb = client.embeddings.create(
    model="nvidia/nemotron-3-embed-1b",
    input=["First document", "Second document"],
)
print(len(emb.data), len(emb.data[0].embedding))
```

## [](https://ailoot.ai/docs/en/connect/openai-sdk/#vision)Image inputs

```python
resp = client.chat.completions.create(
    model="gpt-6.1-sol",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What is in this picture?"},
            {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}},
        ],
    }],
)
```

## [](https://ailoot.ai/docs/en/connect/openai-sdk/#errors)Error handling

Ailoot errors use the OpenAI format, so standard SDK exceptions catch them. The message includes the Request ID.

```python
import openai

try:
    resp = client.chat.completions.create(model="claude-sonnet-5-5", messages=[{"role": "user", "content": "Hello"}])
except openai.APIStatusError as e:
    print(e.status_code, e.response.json()["error"]["code"])
    print(e.response.headers.get("x-request-id"))
```

```js
try {
  await client.chat.completions.create({ model: "claude-sonnet-5-5", messages: [{ role: "user", content: "Hello" }] });
} catch (err) {
  if (err instanceof OpenAI.APIError) {
    console.log(err.status, err.code, err.headers?.["x-request-id"]);
  }
}
```

The OpenAI and Anthropic SDKs usually retry network errors and 5xx responses automatically. Disable this for generation requests (`max_retries=0`) and check `X-Should-Retry` before a new attempt. A lost response does not mean the request was not executed.

Still have questions? Send your Request ID and a description — we'll help you troubleshoot.[Contact support](mailto:support@ailoot.ai)

## API connection

### How do I connect the OpenAI SDK to Ailoot?

Set the base URL to https://api.ailoot.ai/v1. You can set it in code or through the variables OPENAI\_BASE\_URL and OPENAI\_API\_KEY — then your code needs no changes at all.

## Ailoot · The company and its project portfolio





Ailoot combines established operating experience, a large client base, stable service and ongoing KPI control.





The company and its project portfolio: millions of customers; monthly sales in the billions.





Ailoot brings that operating experience to API access for AI features in customer products, agents, applications and automations.
