This tutorial walks through building your autogen first agent gpt-4o n4n.ai integration with Microsoft AutoGen and a single OpenAI-compatible endpoint. You’ll go from an empty virtualenv to a running two-agent chat that solves a coding task, with real output at each checkpoint.
Prerequisites
- Python 3.10 or newer
pipand a shell you control- An API key for the inference gateway, exported as
N4N_API_KEY - Willingness to read stack traces
If you’ve used AutoGen before, you can skip the install step. If you haven’t, the package surface is small enough to learn in one sitting.
Step 1: Stand up a clean environment
Don’t pollute your system Python. Create a venv and install a pinned AutoGen version. The pyautogen 0.2.x line is stable and matches the code below.
python -m venv .venv
source .venv/bin/activate
pip install pyautogen==0.2.32
Verify the import works before writing agent logic:
python -c "import autogen; print(autogen.__version__)"
Expected output:
0.2.32
Step 2: Point AutoGen at the gateway
AutoGen’s OpenAIWrapper accepts a config_list where each entry is a standard OpenAI-style credential block. The only deviation is base_url. The autogen first agent gpt-4o n4n.ai pattern relies on pointing AutoGen’s OpenAI client at n4n.ai’s OpenAI-compatible endpoint, which also gives automatic fallback across providers. You keep the model name gpt-4o; the gateway routes it.
import os
from autogen import AssistantAgent, UserProxyAgent
llm_config = {
"config_list": [{
"model": "gpt-4o",
"base_url": "https://api.n4n.ai/v1",
"api_key": os.environ["N4N_API_KEY"],
}],
"temperature": 0,
}
Set the key first:
export N4N_API_KEY="sk-..."
If you omit base_url, AutoGen hits OpenAI directly. That defeats the purpose of this exercise and loses the fallback and per-token metering the gateway provides.
Step 3: Define a minimal agent pair
AutoGen’s value is in letting a UserProxyAgent execute code while an AssistantAgent generates it. For a first run, disable human input so the loop is fully autonomous.
assistant = AssistantAgent(
name="assistant",
llm_config=llm_config,
system_message="You are a senior Python engineer. Reply with concise, correct code.",
)
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=3,
code_execution_config={"work_dir": "coding"},
)
max_consecutive_auto_reply caps the conversation so a stuck agent doesn’t loop forever. work_dir isolates generated scripts from your repo root.
Step 4: Run the first chat
Kick off a trivial task. The user proxy sends the prompt, the assistant replies with code, and the proxy executes it locally.
chat_result = user_proxy.initiate_chat(
assistant,
message="Write a function that returns the nth Fibonacci number using memoization.",
)
Expected terminal output (truncated for clarity):
user_proxy (to assistant):
Write a function that returns the nth Fibonacci number using memoization.
--------------------------------------------------------------------------------
assistant (to user_proxy):
def fib(n, memo={}):
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
print(fib(10))
--------------------------------------------------------------------------------
>>>>>>>> EXECUTING CODE BLOCK (inferred language: python)...
user_proxy (to assistant):
exitcode: 0 (execution succeeded)
Code output:
55
You now have a working autogen first agent gpt-4o n4n.ai pipeline. The assistant produced a recursive memoized function; the proxy ran it and reported 55 for fib(10).
Step 5: Execute code from a file
Inline execution is fine for demos, but real workflows write files. Prompt the assistant to persist a module and run a test.
user_proxy.send(
assistant,
"Write the fib function to coding/fib.py and add a pytest test in coding/test_fib.py.",
)
The assistant will emit two code blocks. AutoGen’s proxy writes them to disk relative to work_dir. After execution you’ll see:
>>>>>>>> EXECUTING CODE BLOCK (inferred language: python)...
user_proxy (to assistant):
exitcode: 0 (execution succeeded)
Code output:
1 passed in 0.01s
If the model drifts and writes invalid Python, the proxy returns a non-zero exit code and the assistant gets a chance to self-correct, up to the reply cap.
Step 6: Push the agent a bit harder
Agents earn their keep on ambiguous tasks. Try a stateful request:
user_proxy.send(
assistant,
"Refactor coding/fib.py to use a class with a callable instance and keep the test passing.",
)
A competent gpt-4o response looks like:
class Fib:
def __init__(self):
self.memo = {0: 0, 1: 1}
def __call__(self, n):
if n not in self.memo:
self.memo[n] = self(n - 1) + self(n - 2)
return self.memo[n]
fib = Fib()
The proxy reruns the test. If the assistant broke the API, you’ll see a traceback and a follow-up fix attempt. That loop is the entire point of using AutoGen instead of a single completion call.
Inspecting what the gateway did
Because the endpoint is OpenAI-compatible, AutoGen’s usage tracking works unchanged. After a chat, inspect the last message’s cost field if you wrapped the call, or check your gateway dashboard for per-token metering. The gateway honors client routing directives and forwards provider cache-control hints, so repeated identical prefixes in your system prompt are cached without extra code on your side.
If a backing provider rate-limits gpt-4o, the gateway automatically falls back to another routed provider that serves the same model shape. Your AutoGen code does not change. That’s the operational reason to put a gateway in front rather than calling OpenAI’s IP directly.
Practical hardening tips
- Pin the model.
gpt-4ois stable today; snapshot a date-stamped alias in production config so a silent model swap doesn’t change behavior. - Cap replies. Never run
UserProxyAgentwithmax_consecutive_auto_reply=0in unattended mode. Infinite loops cost money and lock workers. - Sandbox the work dir.
code_execution_configruns locally. In CI, wrap it in a container or use DockerCodeExecutor if you’ve moved to AutoGen 0.4. - Log the chat.
chat_result.chat_historyis a JSON-serializable list. Ship it to your logging stack; debugging agent failures without the transcript is guessing.
Where to go next
Swap the single config entry for a list with multiple model fallbacks inside AutoGen itself, or add a third GroupChat agent to review the code before execution. The gateway doesn’t care how many agents you spawn; it bills per token and keeps the OpenAI contract intact. The autogen first agent gpt-4o n4n.ai setup you have now is the same skeleton used for multi-agent pipelines—just more agents and tighter system prompts.