Comparing model families from R with ellmer::chat_openai_compatible() and one gateway endpoint

I work on the APIGOTO team, and APIGOTO is independent of OpenAI, Anthropic and Google. It's an OpenAI-compatible API gateway. Everything below works with any OpenAI-compatible endpoint, though.

ellmer has dedicated functions for most providers. But when I want to run the same prompt across several model families and compare the results, it's simpler to use one chat_openai_compatible() call with one endpoint and only change model. Here's the pattern I use.

Setup

Put the key in ~/.Renviron (usethis::edit_r_environ()) under its own name, so it doesn't collide with an OPENAI_API_KEY you might use for chat_openai():

APIGOTO_API_KEY=your-key-here

One helper

library(ellmer)

gw_chat <- function(model, ...) {
  chat_openai_compatible(
    base_url = "https://api.china-aiapi.com/v1",
    name = "APIGOTO",
    model = model,
    credentials = function() Sys.getenv("APIGOTO_API_KEY"),
    ...
  )
}

chat <- gw_chat("MODEL_ID", system_prompt = "You are a terse R expert.")  # use a model ID from your APIGOTO dashboard
chat$chat("When should I use vctrs::vec_rbind() instead of rbind()?")

Notes from the ellmer docs that matter here:

  • base_url is required for this function. There's no default for OpenAI-compatible APIs.
  • credentials is a zero-argument function, so the key is fetched when needed and never stored in the chat object (which matters if you save chats to disk). api_key is deprecated in favour of it. If you leave out credentials, ellmer falls back to OPENAI_API_KEY.
  • name is what token_usage() shows for the provider.
  • chat_openai_compatible() uses the Chat Completions API. chat_openai() is reserved for OpenAI itself and uses the Responses API.

Same prompt, several models

models <- c("MODEL_ID_A", "MODEL_ID_B", "MODEL_ID_C")
prompt <- "Explain the difference between a tibble and a data.frame in 3 bullet points."

answers <- lapply(models, function(m) {
  ch <- gw_chat(m)
  list(model = m, text = ch$chat(prompt, echo = "none"))
})

token_usage()

token_usage() reports tokens per provider and model for the session, which is the quickest way I've found to see how differently the families count tokens for the same input.

Structured output works the same way, as long as the model behind the endpoint supports it:

gw_chat("MODEL_ID")$chat_structured(
  "Mtcars has 32 rows and 11 columns; it's from 1974 Motor Trend.",
  type = type_object(rows = type_integer(), cols = type_integer(), year = type_integer())
)

If a model doesn't handle JSON-schema output or tool calls well, it shows up here first. Check that before you build a pipeline on it.

Why I route through a gateway

APIGOTO uses one base_url for Claude, GPT and Gemini model families. It fails over to another upstream automatically, with at most 3 retries, which helps when you're looping a prompt over a few hundred rows. Each request also shows up in a per-request log. It's pay-as-you-go, BYOK is supported, and the docs are in English and Chinese: www.china-aiapi.com

Has anyone used parallel_chat() against a compatible gateway at scale? I'm curious how people choose concurrency limits.