# Migrating from OpenAI

> What to change when moving code that calls the OpenAI API over to Lykia; usually the base URL, the API key and the model id are enough.

The Lykia API follows the OpenAI Chat Completions format. When you move an application that uses the OpenAI SDK or plain HTTP requests, your business logic stays as it is. What changes is where the client connects, which key it sends and how model names are written.

## The three changes

1. **Base URL:** `https://api.getlykia.com/v1`
2. **API key:** your Lykia API key (`lyk-v1-…`) instead of the OpenAI key. The [Quickstart](https://www.getlykia.com/en/docs/quickstart) shows how to create one.
3. **Model id:** Lykia writes models as `author/model`. `gpt-4o` becomes `openai/gpt-4o`, and `gpt-4o-mini` becomes `openai/gpt-4o-mini`.

**Python**

```python
import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.getlykia.com/v1",  # was: the default OpenAI endpoint
    api_key=os.environ["LYKIA_API_KEY"],  # was: OPENAI_API_KEY
)

completion = client.chat.completions.create(
    model="openai/gpt-4o",  # was: "gpt-4o"
    messages=[{"role": "user", "content": "Translate 'good morning' into Turkish."}],
)
print(completion.choices[0].message.content)
print("served by:", completion.model_extra.get("provider"), "cost (USD):", getattr(completion.usage, "cost", None))
```

**TypeScript**

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.getlykia.com/v1", // was: the default OpenAI endpoint
  apiKey: process.env.LYKIA_API_KEY, // was: OPENAI_API_KEY
});

const completion = await client.chat.completions.create({
  model: "openai/gpt-4o", // was: "gpt-4o"
  messages: [{ role: "user", content: "Translate 'good morning' into Turkish." }],
});
console.log(completion.choices[0].message.content);

// provider and usage.cost are Lykia additions, so they are read outside the SDK types.
const extra = completion as unknown as { provider: string; usage: { cost: number } };
console.log("served by:", extra.provider, "cost (USD):", extra.usage.cost);
```

If your code creates the client without options, the official OpenAI SDKs read the base URL and the key from environment variables. In that case you can switch to Lykia without touching the code, apart from updating the model ids:

```bash
# The official OpenAI SDKs read these variables when no explicit options are passed.
export OPENAI_BASE_URL="https://api.getlykia.com/v1"
export OPENAI_API_KEY="$LYKIA_API_KEY"
```

## What stays the same

- The request body: `messages`, `temperature`, `max_tokens`, `tools`, `response_format`, `stream` and the other fields keep their names, though a few are not passed on (see [Before you switch](#before-you-switch)). Which models honour a field is listed in their `supported_parameters` in the catalog.
- The response shape: `choices`, `message`, `finish_reason` and `usage` are where you expect them.
- Streaming: Server-Sent Events, `chat.completion.chunk` objects and `data: [DONE]`. See [Streaming](https://www.getlykia.com/en/docs/guides/streaming).
- SDK error types: Lykia errors use OpenAI's error body shape, so the SDK raises them the same way. Status codes are listed in [Errors](https://www.getlykia.com/en/docs/guides/errors).

## Fields Lykia adds

Responses carry a few fields OpenAI does not send. You do not have to use them, and existing code simply ignores them.

- `provider`: the id of the provider that served the request (for example `openai`).
- `usage.cost`: what the request cost, in USD; `usage.is_byok`: whether your own provider key was used; `usage.prompt_tokens_details.cache_write_tokens`: input tokens written to the cache.
- `id`: starts with `gen-`, and comes back in the `X-Generation-Id` response header too. Pass it to [`GET /generation`](https://www.getlykia.com/en/docs/api-reference/generation) for cost and timing details.

There are extra request fields too: `models` for [model fallbacks](https://www.getlykia.com/en/docs/guides/model-fallbacks) and `provider` for [provider preferences](https://www.getlykia.com/en/docs/guides/provider-routing). The SDKs do not know these fields, so send them through `extra_body` in Python, or add them to the body in TypeScript.

## Identify your application

The optional `HTTP-Referer` and `X-Title` headers carry the URL and name of the application sending the request. Lykia stores them in the request log, which helps you tell apps apart when several share a project. They are not forwarded to providers.

```bash
curl https://api.getlykia.com/v1/chat/completions \
  -H "Authorization: Bearer $LYKIA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "HTTP-Referer: https://example.com" \
  -H "X-Title: Example Support Bot" \
  -d '{
    "model": "openai/gpt-4o-mini",
    "messages": [{ "role": "user", "content": "Hello!" }]
  }'
```

## Before you switch

- **Endpoints you call.** Lykia's first release offers the endpoints in the [API reference](https://www.getlykia.com/en/docs/api-reference/overview): chat completions, plus lookups for the catalog, your key, credits and request records. If your code calls other OpenAI endpoints, keep those calls on your current provider for now.
- **Request fields that are not passed on.** On requests that go out with Lykia's keys, Lykia passes on only a fixed list of body fields and drops the rest without an error. Among the dropped fields are `n` (you get a single choice), `service_tier`, `store`, `prediction`, `modalities` and `audio` (no audio output). `user` and `metadata` are Lykia's request tags: they are stored with the record for usage breakdowns and never sent to OpenAI ([Tags and usage breakdown](https://www.getlykia.com/en/docs/guides/tags-and-usage)). The full list is in [What is sent to the provider](https://www.getlykia.com/en/docs/api-reference/chat-completions#what-is-sent-to-the-provider). Requests with your own OpenAI key as a provider key are passed on in full.
- **`max_tokens`.** Lykia may set or lower the output limit so that the request's cost fits the available balance, key limit or project budget, and reports the value in the `X-Max-Tokens-Applied` header. See [Output limit](https://www.getlykia.com/en/docs/api-reference/chat-completions#output-limit).
- **Model ids.** Look up the catalog id of every OpenAI model you use with [`GET /models`](https://www.getlykia.com/en/docs/api-reference/models). The catalog also lists models from other authors, so the same code can try them.
- **Paying.** Paid models need a credit balance on your company. If you would rather keep using your OpenAI key, add it to the project as a provider key: requests to OpenAI models then go out with that key and OpenAI bills you. See [Bring your own key (BYOK)](https://www.getlykia.com/en/docs/guides/byok).
- **Privacy.** By default Lykia does not store prompt or response text. Details in [Privacy and logging](https://www.getlykia.com/en/docs/guides/privacy-and-logging).

## Next steps

- [Provider routing](https://www.getlykia.com/en/docs/guides/provider-routing): choose which providers serve your requests.
- [Models](https://www.getlykia.com/en/docs/models): listing the catalog and price units.
- [`POST /chat/completions`](https://www.getlykia.com/en/docs/api-reference/chat-completions): every field of the request body.
