Why one function is enough

Most providers now copy OpenAI’s chat format. That means the request body — a model plus a messages array — is identical across Groq, OpenRouter and Gemini’s compatibility endpoint. If you read our OpenRouter guide, this is the same idea applied to more providers.

The request builder

Keep your key safe

The example deliberately doesn’t send anything. In production, put fetch(req.url, …) inside a server or edge function and read the key from an environment variable. Shipping a key in browser code is the #1 way keys get stolen.

Plug it into an agent

Swap the fake_llm() stub in the 50-line agent for this builder, ask the model to answer in JSON, and you have a real tool-calling agent. Not sure which model? See Grok vs ChatGPT vs Claude vs Gemini for agents.