← All integrationsIntegration · SDK

Amazon Ergo TypeScript quickstart

Our API for Amazon Ergo follows the OpenAI Chat Completions shape, so the regular openai npm package works once you change the baseURL. This quickstart covers install, a plain request, a streaming request and how to read token usage, including reasoning tokens.

Every example needs an ergo_live_ API key with prepaid credit (from $5). Get an API key →

There is no dedicated client library to learn. The openai package sends the same JSON to any OpenAI-compatible server, and that is what https://ergomodel.net/api/v1 is. An existing OpenAI codebase moves over by changing two fields, the base URL and the key, plus the model name ergo-1.0. Ergo Model is an independent hosted service; it is not operated by Amazon or by OpenAI.

1. Install

npm i openai

2. Set your key

Create an ergo_live_ key at /get-api-key and keep it in the environment. Calls are prepaid: add credit from $5, then pay $1.50 per 1M input tokens and $4.50 per 1M output tokens.

export ERGO_API_KEY=ergo_live_...

3. A non-streaming request

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://ergomodel.net/api/v1",
  apiKey: process.env.ERGO_API_KEY,
});

const resp = await client.chat.completions.create({
  model: "ergo-1.0",
  max_tokens: 2048,
  messages: [
    { role: "system", content: "You are a concise coding assistant." },
    { role: "user", content: "Rewrite this function so it has no side effects: ..." },
  ],
});

console.log(resp.choices[0].message.content);
console.log(resp.choices[0].finish_reason);

Why max_tokens is 2048 here

Ergo always reasons before it answers and you cannot disable that; a request that tries gets HTTP 400. Reasoning tokens come out of max_tokens and are billed as output. We measured roughly 300 to 450 reasoning tokens for a one-line haiku, so a cap of 300 returns an empty content string with finish_reason "length". Use 2,048 or more for short answers; AWS's own guidance is an output limit of 8,192 or higher. To spend less on thinking, lower the effort:

const resp = await client.chat.completions.create({
  model: "ergo-1.0",
  max_tokens: 4096,
  // not in the SDK's types, but forwarded as-is by the API
  // @ts-expect-error extra field
  reasoning: { effort: "low" }, // "low" | "medium" | "high", or { max_tokens: N }
  messages: [{ role: "user", content: "Name three uses for a WeakMap." }],
});

4. Streaming

Set stream: true and iterate the chunks. Our endpoint always sends a final usage frame on streams; that last chunk has an empty choices array, so guard the access.

const stream = await client.chat.completions.create({
  model: "ergo-1.0",
  max_tokens: 4096,
  stream: true,
  messages: [{ role: "user", content: "Explain the Node event loop step by step." }],
});

let usage;
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
  if (chunk.usage) usage = chunk.usage;
}
console.log("\n", usage);

5. Reading usage

Every response carries a usage object. prompt_tokens is billed as input and completion_tokens as output. When the upstream provider reports it, completion_tokens_details.reasoning_tokens shows how much of the output was reasoning; that number is already included in completion_tokens, not added on top.

const u = resp.usage;
const reasoning = u?.completion_tokens_details?.reasoning_tokens ?? 0;

console.log({
  input: u?.prompt_tokens,
  output: u?.completion_tokens,
  reasoning,
  visible: (u?.completion_tokens ?? 0) - reasoning,
});

// rough cost in USD at $1.50 in / $4.50 out per 1M tokens
const cost = ((u?.prompt_tokens ?? 0) * 1.5 + (u?.completion_tokens ?? 0) * 4.5) / 1e6;

Errors worth handling

All errors use the shape {"error": {"message", "type", "code"}}, which the SDK surfaces as an APIError with a status field.

Limits to keep in mind

Without the SDK

It is a plain HTTP call, so fetch works too:

const r = await fetch("https://ergomodel.net/api/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${process.env.ERGO_API_KEY}`,
  },
  body: JSON.stringify({
    model: "ergo-1.0",
    max_tokens: 2048,
    messages: [{ role: "user", content: "Hello!" }],
  }),
});
const data = await r.json();
console.log(data.choices[0].message.content);

FAQ

Do I need a special SDK to call Amazon Ergo from TypeScript?

No. The openai npm package works: set baseURL to https://ergomodel.net/api/v1, pass an ergo_live_ key as apiKey, and use the model name ergo-1.0.

Why is my response content empty?

max_tokens is too small. The model always reasons first and reasoning tokens count against max_tokens, so a low cap is used up before any visible text is written and the reply ends with finish_reason "length". Set max_tokens to 2,048 or more.

How do I see reasoning tokens?

Read usage.completion_tokens_details.reasoning_tokens when it is present. Those tokens are part of completion_tokens and are billed at the output rate.

Does streaming work?

Yes. Pass stream: true and read chunk.choices[0].delta.content. The stream ends with a usage frame whose choices array is empty.

What does it cost?

$1.50 per 1M input tokens and $4.50 per 1M output tokens, prepaid from $5, no subscription. A 100,000-token prompt costs $0.15 of input; 2,000 output tokens cost $0.009.

Can I send images?

No. Amazon Ergo is text in, text out.

See also: API reference · OpenAI-compatible API · Rate limits · Get an API key

Start using Amazon Ergo

Try the model in the free browser playground first, then create a key and add it to your setup.

Open the playgroundGet an API key
Amazon Ergo TypeScript quickstart with the openai SDK · Ergo Model