n4nAI

AutoGen Studio setup with n4n.ai as the model provider

Step-by-step guide to autogen studio setup n4n.ai provider as an OpenAI-compatible model endpoint, from install to verified run with code.

n4n Team4 min read875 words

Audio narration

Coming soon — every post will get a voice note here.

Getting a working autogen studio setup n4n.ai provider is straightforward because n4n.ai exposes an OpenAI-compatible endpoint, so AutoGen Studio’s existing OpenAI model adapter works without patching. This walkthrough takes you from a clean Python environment to a verified multi-agent chat that routes through that gateway, including the exact fields Studio needs and a direct client check.

Step 1: Install and launch AutoGen Studio

Use a dedicated virtual environment. AutoGen Studio is a FastAPI + React app shipped as a single CLI; mixing it with an existing AutoGen codebase often creates version conflicts on autogen-core.

python -m venv autogen-env
source autogen-env/bin/activate
pip install --upgrade pip
pip install autogenstudio
autogenstudio ui --port 8080 --host 127.0.0.1

The UI is served at http://127.0.0.1:8080. On first launch it initializes a local SQLite store for models, flows, and sessions—no external DB required. If you need to reset the state, stop the process and delete the autogenstudio.db file in the working directory.

Step 2: Acquire and export the API key

The gateway uses a single bearer token for all 240+ models behind it. Generate the key from the provider dashboard and export it as an environment variable so you are not pasting secrets into the browser repeatedly:

export N4N_API_KEY="sk-xxxxxxxxxxxxxxxx"

You will copy this value into the Studio UI once. AutoGen Studio does not read environment variables for model credentials at runtime—it persists them in its DB—but keeping the value in your shell history as a variable is safer than typing it into a form multiple times.

Step 3: Register the model endpoint

For the autogen studio setup n4n.ai provider, point AutoGen Studio’s OpenAIChat adapter at the gateway’s base URL. In the UI, open the Models tab and click Add Model. Fill the form as follows:

  • Model Type: OpenAIChat
  • Model Name: gpt-4o-mini (or any routed model ID; prefix with openai/ if you want to pin the upstream)
  • API Key: $N4N_API_KEY value
  • Base URL: https://api.n4n.ai/v1
  • API Version: leave blank (not Azure)
  • Other kwargs (JSON): {"temperature": 0.1}

The model name field is passed verbatim to the gateway. Because the gateway honors client routing directives, a bare gpt-4o-mini lets it select the best available upstream, while openai/gpt-4o-mini forces a specific provider. This matters when you want deterministic routing for eval suites.

Why the OpenAI adapter works unchanged

The gateway is wire-compatible with the OpenAI chat completions contract: same path (/chat/completions), same request shape, same streaming chunks. AutoGen Studio’s OpenAIChat client sets Authorization: Bearer and posts JSON; nothing in the request needs middleware. The gateway also forwards provider cache-control hints, so if you later attach cache_control markers in AutoGen messages, they reach the upstream.

Step 4: Build a minimal agent flow

Studio’s visual editor is fine for a smoke test. Create a new flow with two agents:

  1. UserProxyAgent — set human_input_mode: NEVER, max_consecutive_auto_reply: 1.
  2. AssistantAgent — bind it to the model you registered in Step 3.

Connect them with a InitiateChat node. Set the initial message to:

Return a JSON object with keys "status" and "echo", echoing the word "pong".

Hit Run. Within a few seconds the Assistant agent should return parsed JSON. If you see a 401, the key is wrong; a 404 means the model ID is not routed; a 424 indicates the gateway’s upstream is degraded and fallback did not resolve—rare, because the gateway performs automatic fallback when a provider is rate-limited.

Step 5: Verify outside the UI

Studio hides the raw HTTP exchange. Before trusting it in a pipeline, confirm the endpoint independently with the openai Python client. This removes Studio’s SQLite and React layers from the equation.

from openai import OpenAI

client = OpenAI(
    base_url="https://api.n4n.ai/v1",
    api_key="sk-xxxxxxxxxxxxxxxx",
)

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Reply with the single word: ok"}],
    temperature=0,
)
print(resp.choices[0].message.content)
print("usage:", resp.usage)

A successful run prints ok and a usage object with prompt_tokens / completion_tokens. The usage metering is per-token and attributed to your gateway account, so the numbers you see here match what Studio would have consumed for the same call.

Verify model routing explicitly

If you need to prove which upstream served the request, pass the prefixed model ID:

resp = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "which model are you?"}],
)

The gateway will route only to OpenAI’s deployment of that model and return its native response. This is the same string you would put in Studio’s Model Name field.

Step 6: Persist the configuration for CI

For headless environments, you can pre-seed Studio’s DB by hitting its REST API before launch, but the supported path is to script the agent directly with autogen core using the same base URL. That keeps your autogen studio setup n4n.ai provider logic portable:

from autogen import AssistantAgent, UserProxyAgent, OpenAIWrapper

llm_config = {
    "config_list": [
        {
            "model": "gpt-4o-mini",
            "base_url": "https://api.n4n.ai/v1",
            "api_key": "sk-xxxxxxxxxxxxxxxx",
        }
    ],
    "temperature": 0,
}

assistant = AssistantAgent("assistant", llm_config=llm_config)
user = UserProxyAgent("user", human_input_mode="NEVER", max_consecutive_auto_reply=1)
user.initiate_chat(assistant, message="Ping.")

This uses the same OpenAI-compatible contract; if it runs, Studio will run.

Troubleshooting notes

  • CORS errors in browser: Studio calls its own backend, which proxies to the gateway. If you see CORS failures, you pointed Studio’s model at a browser-exposed URL instead of letting the backend forward. The Base URL field is used server-side; that is correct.
  • Timeout on first call: Cold routes through the gateway may take a few seconds while it resolves an upstream. Raise Studio’s request timeout by setting AUTOGENSTUDIO_TIMEOUT=30 in the environment before launch.
  • Streaming artifacts: AutoGen Studio expects SSE chunks with choices[0].delta. The gateway is compliant; if you see truncated text, check that you did not set stream: false in the Other kwargs JSON.

Closing verification checklist

You are done when: (1) the Studio flow returns valid JSON from the Assistant, (2) the standalone openai client prints ok and non-zero usage, and (3) the autogen core script completes a chat without exceptions. At that point the gateway is acting as a drop-in OpenAI substitute for every AutoGen Studio feature—tool calls, group chat, and code execution—because none of those touch the model protocol beyond chat completions.

Tagsautogen-studion4n-aisetupmodel-provider

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All autogen getting started with n4n.ai posts →