Migrate to Koah
Keep your SDK. Connect to free inference. Show ads when Koah requests them.
Already using the OpenAI or Anthropic SDK? Update your connection settings and choose a supported model. Then connect the returned ad ID to a visible placement in your app. Ads fund your input and output tokens.
Migrate this app's inference integration to Koah. Koah free inference is in alpha, and Koah must grant access to it. Confirm with me that this app is in the alpha program before you change code. If it is not, stop and tell me to apply at https://www.koahlabs.com/inference. Read https://docs.koahlabs.com/inference/migrate and its linked guides, and follow them. Do not invent APIs that the guides do not document. Run the app's checks and report any unsupported features or untested paths.
What changes
| Setting | Update |
|---|---|
| API key | Use your Koah inference key through KOAH_INFERENCE_API_KEY. Keep it on the server. |
| OpenAI SDK base URL | https://inference.koah.ai/v1 |
| Anthropic SDK base URL | https://inference.koah.ai — the SDK adds /v1/messages. |
| Model | Use a model ID from Models, such as sail/DeepSeek-V4.1-Flash-1017-free. |
| Response handling | Read X-Koah-Ad-Id and pass it to your client with the generated content. |
| Ad placement | Use the Koah SDK to render an ad only when an ID is returned. |
Keep your prompts and application logic. Check model support for tools, structured output, images, and streaming before relying on the same behavior. Using the Anthropic SDK does not mean you are calling a Claude model.
Update your client
Set your new key in the server environment:
API key
export KOAH_INFERENCE_API_KEY="YOUR_KOAH_INFERENCE_KEY"
Choose the API your app already uses. These examples keep the familiar request format and add access to the response headers. Red lines are removed; green lines are added. The copy button copies the updated code without diff markers.
import os
from openai import OpenAI
client = OpenAI(
Removed: base_url="https://api.openai.com/v1",
Added: base_url="https://inference.koah.ai/v1",
Removed: api_key=os.environ["OPENAI_API_KEY"],
Added: api_key=os.environ["KOAH_INFERENCE_API_KEY"],
)
Removed: completion = client.chat.completions.create(
Added: raw = client.chat.completions.with_raw_response.create(
Removed: model="your-current-model",
Added: model="sail/DeepSeek-V4.1-Flash-1017-free",
messages=[{"role": "user", "content": "What is inference in machine learning?"}],
)
Added: completion = raw.parse()
print("Generated Content:", completion.choices[0].message.content)
Added: print("Ad ID:", raw.headers.get("X-Koah-Ad-Id"))
Update the same settings in your deployment, background workers, and any other places that call the model. See the API reference for endpoint details. Check support separately if your app uses another API, such as OpenAI Responses.
Connect the ad placement
Return the generated content and the header value as adId from your server.
An absent header becomes adId: null. The inference response does not contain
an ad payload, and ads stay separate from the model's content.
Install the Koah SDK for your app with your public publisher ID. For a web
app, pass the ID to the proposed displayAd method after showing the content:
Client · proposed API
contentTarget.textContent = content
Added: if (adId) {
Added: await window.koah.displayAd(adId, { target: adTarget })
Added: }
Render the ad when Koah returns an ad ID. Not every response requests an ad. When one does, show it in a visible placement beside the content. Deliberately hiding or skipping a requested ad violates the Terms of Service for free inference.
For streaming, wait until the content finishes before rendering the ad. Keep the content visible if ad delivery fails. Follow Build an app for a complete server and client example, or the quickstart for other app SDKs.
Check the full flow
- Confirm the model returns the content your app expects. Test any tools, structured output, or images your app uses.
- Confirm your server passes through an ad ID when one is returned, and your client renders that ad in a visible slot.
- Confirm responses without an ad ID show content without calling the renderer.
- Check Rate Limits before moving your traffic to Koah.