This template is an OpenAI Agents SDK starter that runs on Neon Functions and Neon AI Gateway. AI Gateway is OpenAI-compatible, so the template uses the branch's gateway credentials instead of an OpenAI API key.
About this template
Stream an agent replyRecommended
POST messages to stream a tool-using agent reply as plain text.
/chat
Hand off to a specialistRecommended
POST a request to a triage agent that hands it to a billing or technical agent.
/triage
Return structured output
POST text to get a typed JSON object using the agent's outputType schema.
/extract
Scaffold it
npm install -g neon@latest
neon login
neon link
neon functions new openai-agentsThe CLI asks which routes to include, and --all-operations includes /extract too. It writes the modules and a router index.ts under functions/openaiagents/ and declares the function in neon.ts.
Enable AI Gateway on the linked branch:
neon config add ai-gatewayAI Gateway requires a paid Neon plan with prepaid credits and a project in a supported region. See the AI Gateway quickstart.
Deploy once so Neon provisions the gateway and pulls NEON_AI_GATEWAY_BASE_URL and NEON_AI_GATEWAY_TOKEN into .env.local:
neon deploy --env .env.localRun locally
neon devneon dev loads .env.local, injects the branch's AI Gateway credentials, and prints the URL to call. The examples below use $URL for that URL.
Chat
curl -N -X POST "$URL/chat" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Roll 3 dice and tell me the total."}]}'The response is a plain-text stream.
Triage
curl -X POST "$URL/triage" \
-H "Content-Type: application/json" \
-d '{"message":"I was charged twice for my subscription."}'Example response:
{
"agent": "billing",
"reply": "I'm sorry about the duplicate charge. ..."
}Extract
curl -X POST "$URL/extract" \
-H "Content-Type: application/json" \
-d '{"text":"Ada Lovelace has user ID 12345, and signed up with ada@example.com. She has been a member since 2026-01-04."}'Example response:
{
"data": {
"name": "Ada Lovelace",
"email": "ada@example.com",
"joinedAt": "2026-01-04"
},
"model": "gpt-5-mini"
}Change ContactSchema in extract.ts to extract the fields your app needs.
Authentication
Function URLs are public, and this template doesn't add authentication, so anyone with the URL can call these routes and spend AI Gateway credits. Before you expose the function, check a bearer token in each route or use Managed Better Auth.
Errors
| Status | Meaning |
|---|---|
400 | The body is not JSON or fails validation. |
405 | The method is not POST. |
502 | The model request failed. |
503 | AI Gateway is not enabled. |
/chat returns 200 as soon as the stream starts. A model error after that truncates the stream, and the function logs the error.
Customize
- Model. Every module sets
MODEL = "gpt-5-mini". You can use any tool-calling model from the AI Gateway catalog. - Chat tool. Edit
rollDiceinchat.ts, or replace it with a tool that reads your own data. - Triage agents. Edit the instructions and
handoffsintriage.ts. - Structured output. Edit
ContactSchemainextract.ts. The module passes the schema asoutputType, so the SDK validates the final output before it reaches your code. - Responses API.
useResponses: falsekeeps every route on Chat Completions, which works with the whole catalog. For Responses-only features, see OpenAI Responses on AI Gateway. Use/openai/v1anduseResponses: true, and check that your model supports the Responses API.








