> For the complete documentation index, see [llms.txt](https://docs.cortecs.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cortecs.ai/integration-examples/tooling/openai-sdk.md).

# OpenAI SDK

The official OpenAI Python and JavaScript SDKs can call Cortecs through its compatible APIs. Prefer the Responses API for new agent workflows and use Chat Completions when a library or model requires it.

{% hint style="info" %}
Create a Cortecs API key by following the [Quickstart](/quickstart.md), then choose a model with the required capabilities from the [Models API](/api-overview/models.md).
{% endhint %}

## Responses API

{% tabs %}
{% tab title="Python" %}
Install the SDK:

```bash
pip install openai
```

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.cortecs.ai/v1",
    api_key=os.environ["CORTECS_API_KEY"],
)

response = client.responses.create(
    model=os.environ["CORTECS_MODEL"],
    instructions="Answer concisely.",
    input="Name three uses for semantic search.",
    extra_body={"preference": "balanced"},
)

print(response.output_text)
```

{% endtab %}

{% tab title="JavaScript" %}
Install the SDK:

```bash
npm install openai
```

```javascript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.cortecs.ai/v1",
  apiKey: process.env.CORTECS_API_KEY,
});

const response = await client.responses.create({
  model: process.env.CORTECS_MODEL,
  instructions: "Answer concisely.",
  input: "Name three uses for semantic search.",
});

console.log(response.output_text);
```

{% endtab %}
{% endtabs %}

The JavaScript SDK does not expose Cortecs routing fields as typed parameters. Set project defaults in Cortecs or use a client that supports custom request body fields when per-request routing is required.

## Chat Completions fallback

Use this shape when a dependency only supports Chat Completions:

```python
completion = client.chat.completions.create(
    model=os.environ["CORTECS_MODEL"],
    messages=[
        {"role": "system", "content": "Answer concisely."},
        {"role": "user", "content": "Name three uses for semantic search."},
    ],
    extra_body={"preference": "balanced"},
)

print(completion.choices[0].message.content)
```

The fallback endpoint is `https://api.cortecs.ai/v1/chat/completions`. See [API Compatibility](/api-overview/api-compatibility.md) before translating more advanced Responses requests.

For request-shape differences, see OpenAI's [Responses migration guide](https://developers.openai.com/api/docs/guides/migrate-to-responses). Cortecs compatibility is limited to the fields in the Cortecs API reference.
