Happy Pony API Get an API key →

Chat completions (OpenAI-compatible)

Pricing: Chat completions

Our hosted LLM speaks the OpenAI chat completions protocol, so any OpenAI SDK or OpenAI-compatible tool works by changing two settings:

Setting Value
Base URL https://api.happypony.io/v1
API key your hp_… key (sent as Authorization: Bearer hp_…)
Model the one id GET /v1/models returns (qwen3.8-27b on api.happypony.io at the time of writing; each deployment names its own)

The request and response are passed through with only the proxy's own bookkeeping added (usage reporting on streams, session affinity for the upstream): messages (text and image_url content parts), tools / tool_choice (tool calling), temperature, max_tokens, response_format, reasoning / reasoning_effort, stream, stream_options, and anything else the model understands (llama.cpp's chat_template_kwargs is translated for upstreams that do not know it, and a field the upstream rejects as unknown is dropped and the request retried). Leave model out or send any id: the proxy accepts every model id and forwards the request to its configured upstream model. Each response carries an x-request-id header (chatcmpl-…) that also appears in your usage rows, except the 401, 413 and 503 answers, which are sent before a request is recorded.

Streaming. With "stream": true the answer arrives as server-sent events (data: {…chunk…} lines, ending with data: [DONE]). Send "stream_options": {"include_usage": true} to receive a final chunk with usage (and an empty choices array); without it, no such chunk is sent.

Non-streaming answers. Without stream, the response is one chat.completion JSON object, but its headers are sent as soon as the model starts and the body begins with a few newline characters (one every 15 s) while the model is still thinking, so a long answer is not cut off by an HTTP timeout somewhere between you and us. Every JSON parser skips that leading whitespace. If the model fails after the headers went out, the body is {"error": {…, "code": "upstream_error"}} under HTTP 200 instead of a choices array — check for error if you do not use an SDK.

Python (openai SDK)

# pip install openai   |   HP_KEY=hp_… python chat.py
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.happypony.io/v1", api_key=os.environ["HP_KEY"])

# One answer.
r = client.chat.completions.create(
    model="qwen3.8-27b",
    messages=[{"role": "user", "content": "Write a haiku about ponies."}],
)
print(r.choices[0].message.content, r.usage)

# Streaming.
stream = client.chat.completions.create(
    model="qwen3.8-27b",
    messages=[{"role": "user", "content": "Count to five."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

# Tool calling.
tools = [{"type": "function", "function": {
    "name": "get_weather",
    "description": "Current weather for a city",
    "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
}}]
r = client.chat.completions.create(
    model="qwen3.8-27b",
    messages=[{"role": "user", "content": "Weather in Paris?"}],
    tools=tools,
)
print(r.choices[0].message.tool_calls)

Node.js (openai package)

// npm install openai   |   HP_KEY=hp_… node chat.mjs
import OpenAI from 'openai';

const client = new OpenAI({ baseURL: 'https://api.happypony.io/v1', apiKey: process.env.HP_KEY });

const stream = await client.chat.completions.create({
  model: 'qwen3.8-27b',
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'What is in this picture?' },
        { type: 'image_url', image_url: { url: 'https://example.com/pony.jpg' } },
      ],
    },
  ],
  stream: true,
});
for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta?.content ?? '');

curl

curl https://api.happypony.io/v1/chat/completions \
  -H "Authorization: Bearer $HP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3.8-27b", "messages": [{"role": "user", "content": "Hello!"}]}'

# Streaming (-N: don't buffer).
curl -N https://api.happypony.io/v1/chat/completions \
  -H "Authorization: Bearer $HP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3.8-27b", "stream": true, "messages": [{"role": "user", "content": "Hello!"}]}'

Models

GET /v1/models lists the configured model; GET /v1/models/<any-id> returns that model. POST /v1/chat/completions accepts any model id and forwards the request to the configured upstream.

{ "object": "list", "data": [{ "id": "qwen3.8-27b", "object": "model", "created": 1793000000, "owned_by": "happypony" }] }

qwen3.8-27b is a thinking model: it reasons before it answers, and the reasoning is returned as reasoning_content on the message (or on the stream deltas). Those tokens count towards max_tokens, so give it room — with max_tokens below ~100 the reasoning alone can exhaust the budget and content comes back empty with finish_reason: "length". The model accepts image inputs (image_url content parts), tools and tool_choice: "auto" | "none" | "required"; forcing a single named function via tool_choice: {"type": "function", ...} is not honoured and behaves like "auto".

To switch reasoning off send reasoning_effort: "none" or reasoning: {"enabled": false} (llama.cpp's chat_template_kwargs: {"enable_thinking": false} is accepted too and translated); the answer then has no reasoning_content and costs fewer output tokens. If the request is served by the backup model (only while the primary is unavailable) those fields are dropped and the answer reasons, because the backup cannot switch it off. Messages must follow the model's chat template: exactly one system message, and only as the first message, otherwise the request is rejected with a 400 whose code is invalid_messages (not failed over).

Usage and cost

GET /api/v1/account/llm-usage (API key, or signed in on www.happypony.io) returns your requests in a time window with their tokens and cost.

Query Default
from, to the last 30 days ISO dates; from inclusive, to exclusive
limit 50 rows per page, at most 200
offset 0
{
  "totals": { "requests": 2, "prompt_tokens": 2000, "cached_tokens": 400, "completion_tokens": 1000, "cost_usd": 0.00094, "cost_credits": 0.094 },
  "pricing": { "model": "qwen3.8-27b", "input_usd_per_m": 0.2, "cached_usd_per_m": 0.05, "output_usd_per_m": 0.6 },
  "rows": [
    { "id": 12, "request_id": "chatcmpl-…", "created_at": "2026-10-26T10:00:00.000Z", "model": "qwen3.8-27b",
      "upstream": "primary", "stream": true, "status": 200, "prompt_tokens": 1000, "cached_tokens": 200,
      "completion_tokens": 500, "usage_source": "upstream", "cost_usd": 0.00047, "latency_ms": 2310, "ttft_ms": 420, "error": null }
  ]
}

A malformed from, to, limit or offset is 400 invalid_request. totals cover the whole window, rows are one page of it, newest first. usage_source is upstream (the model's own token counts), estimated (~4 characters per token, when the model never reported them) or none (the request never reached the model). pricing is null when no price is configured.

Errors

Errors use OpenAI's shape, so SDKs raise them as the matching exception: {"error": {"message": "…", "type": "…", "code": "…"}}.

Status code When
400 — (type: invalid_request_error) Bad JSON, no messages, a non-boolean stream; or the model refused the parameters (any 4xx other than a dropped unknown field is passed through as the model sent it, not failed over).
400 invalid_messages More than one system message, or one that is not first (see Models).
401 invalid_api_key Missing, wrong or revoked API key.
402 insufficient_credits Balance below the minimum to start a request, checked before the body is read. Top up at www.happypony.io/payment.
404 unknown_url A path under /v1/ other than chat completions and models.
413 request_too_large Body above 16 MiB (large images: send a URL rather than base64).
429 concurrency_limit Too many requests in flight on your account (16). Retry after retry-after seconds.
502 upstream_error The model is unavailable. Retry.
503 not_configured The API is not enabled on the server, or no price is configured for the model.

A stream that breaks after it has started ends with an {"error": {…}} chunk instead of [DONE]: the model's own type and code when it sent an error, upstream_error otherwise.

Chat completions(OpenAI 兼容)

价格:Chat completions

我们托管的 LLM 使用 OpenAI chat completions 协议,任何 OpenAI SDK 或 OpenAI 兼容工具只需改两个设置即可使用:

设置 取值
Base URL https://api.happypony.io/v1
API key 你的 hp_… key(以 Authorization: Bearer hp_… 发送)
模型 GET /v1/models 返回的唯一 id(撰写时 api.happypony.io 上为 qwen3.8-27b;每个部署有自己的名称)

请求和响应基本原样透传,只加上代理自身的簿记(流式用量上报、上游会话亲和):messages(文本和 image_url 内容块)、tools / tool_choice(工具调用)、temperature、max_tokens、response_format、 reasoning / reasoning_effort、stream、stream_options,以及模型支持的 其他任何字段(llama.cpp 的 chat_template_kwargs 会为不认识它的上游自动转换; 上游拒绝的未知字段会被去掉并重试一次)。model 可以省略或填上面的 id; 其他 id 也会被接受,代理会把请求转发到已配置的上游模型。每个响应都带有 x-request-id 头 (chatcmpl-…),它也会出现在你的用量记录里;401、413 和 503 响应除外,它们在请求被记录前就已返回。

流式输出。 传 "stream": true 时,回答以 server-sent events 形式返回 (data: {…chunk…} 行,最后是 data: [DONE])。传 "stream_options": {"include_usage": true} 可在最后多收到一个带 usage (choices 为空数组)的 chunk;不传则不会有这个 chunk。

非流式回答。 不传 stream 时,响应是一个完整的 chat.completion JSON 对象,但响应头会在模型开始生成时就先发出,正文开头会在模型思考期间先出现 几个换行符(每 15 秒一个),以免长回答被你我之间某个 HTTP 超时截断。任何 JSON 解析器都会跳过这些开头的空白。如果响应头发出后模型出错,正文会是 HTTP 200 下的 {"error": {…, "code": "upstream_error"}} 而不是 choices 数组——不用 SDK 的话请检查 error 字段。

Python(openai SDK)

# pip install openai   |   HP_KEY=hp_… python chat.py
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.happypony.io/v1", api_key=os.environ["HP_KEY"])

# 一次回答。
r = client.chat.completions.create(
    model="qwen3.8-27b",
    messages=[{"role": "user", "content": "写一首关于小马的俳句。"}],
)
print(r.choices[0].message.content, r.usage)

# 流式输出。
stream = client.chat.completions.create(
    model="qwen3.8-27b",
    messages=[{"role": "user", "content": "从 1 数到 5。"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

# 工具调用。
tools = [{"type": "function", "function": {
    "name": "get_weather",
    "description": "某个城市的当前天气",
    "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
}}]
r = client.chat.completions.create(
    model="qwen3.8-27b",
    messages=[{"role": "user", "content": "巴黎现在天气怎么样?"}],
    tools=tools,
)
print(r.choices[0].message.tool_calls)

Node.js(openai 包)

// npm install openai   |   HP_KEY=hp_… node chat.mjs
import OpenAI from 'openai';

const client = new OpenAI({ baseURL: 'https://api.happypony.io/v1', apiKey: process.env.HP_KEY });

const stream = await client.chat.completions.create({
  model: 'qwen3.8-27b',
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: '这张图里有什么?' },
        { type: 'image_url', image_url: { url: 'https://example.com/pony.jpg' } },
      ],
    },
  ],
  stream: true,
});
for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta?.content ?? '');

curl

curl https://api.happypony.io/v1/chat/completions \
  -H "Authorization: Bearer $HP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3.8-27b", "messages": [{"role": "user", "content": "你好!"}]}'

# 流式输出(-N:不缓冲)。
curl -N https://api.happypony.io/v1/chat/completions \
  -H "Authorization: Bearer $HP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3.8-27b", "stream": true, "messages": [{"role": "user", "content": "你好!"}]}'

模型

GET /v1/models 列出已配置的模型;GET /v1/models/<任意 id> 返回该模型。POST /v1/chat/completions 接受任意 model id,并转发到已配置的上游。

{ "object": "list", "data": [{ "id": "qwen3.8-27b", "object": "model", "created": 1793000000, "owned_by": "happypony" }] }

qwen3.8-27b 是思考模型:回答前会先推理,推理内容通过 message(或流式 delta)上的 reasoning_content 返回。这些 token 计入 max_tokens,所以请留足余量——max_tokens 低于 100 左右时,推理可能耗尽预算,content 为空且 finish_reason 为 "length"。模型支持图片输入(image_url 内容块)、tools 以及 tool_choice: "auto" | "none" | "required";通过 tool_choice: {"type": "function", ...} 强制调用某个具名函数不生效,行为等同于 "auto"。

要关闭推理,发送 reasoning_effort: "none" 或 reasoning: {"enabled": false}(llama.cpp 的 chat_template_kwargs: {"enable_thinking": false} 也接受,并会自动转换),返回中不再有 reasoning_content,输出 token 也更少。若请求由备用模型处理(仅在主模型不可用时),这些字段会被去掉、回答仍带推理,因为备用模型无法关闭推理。消息必须符合模型的聊天模板:只能有一条 system 消息,且必须是第一条,否则请求会被拒绝并返回 400,code 为 invalid_messages(不会切换到备用模型)。

用量与费用

GET /api/v1/account/llm-usage(API key,或在 www.happypony.io 登录后) 返回一个时间窗口内你的请求及其 token 数和费用。

查询参数 默认值 说明
from、to 最近 30 天 ISO 日期;from 含,to 不含
limit 50 每页行数,最多 200
offset 0
{
  "totals": { "requests": 2, "prompt_tokens": 2000, "cached_tokens": 400, "completion_tokens": 1000, "cost_usd": 0.00094, "cost_credits": 0.094 },
  "pricing": { "model": "qwen3.8-27b", "input_usd_per_m": 0.2, "cached_usd_per_m": 0.05, "output_usd_per_m": 0.6 },
  "rows": [
    { "id": 12, "request_id": "chatcmpl-…", "created_at": "2026-10-26T10:00:00.000Z", "model": "qwen3.8-27b",
      "upstream": "primary", "stream": true, "status": 200, "prompt_tokens": 1000, "cached_tokens": 200,
      "completion_tokens": 500, "usage_source": "upstream", "cost_usd": 0.00047, "latency_ms": 2310, "ttft_ms": 420, "error": null }
  ]
}

from、to、limit 或 offset 格式错误时返回 400 invalid_request。totals 覆盖整个时间窗口,rows 是其中一页,最新的在前。usage_source 为 upstream(模型自己统计的 token 数)、estimated(模型没有报告时按约 4 个字符一个 token 估算)或 none(请求没有到达模型)。未配置价格时 pricing 为 null。

错误

错误采用 OpenAI 的格式,SDK 会抛出对应的异常: {"error": {"message": "…", "type": "…", "code": "…"}}。

状态码 code 何时出现
400 —(type: invalid_request_error) JSON 无效、没有 messages、stream 不是布尔值;或模型拒绝了参数(除被去掉的未知字段外,任何 4xx 都按模型返回的内容原样透传,不切换备用模型)。
400 invalid_messages 有多条 system 消息,或它不是第一条(见模型)。
401 invalid_api_key API key 缺失、错误或已吊销。
402 insufficient_credits 余额低于发起请求的最低要求,在读取请求体之前检查。请到 www.happypony.io/payment 充值。
404 unknown_url /v1/ 下除 chat completions 和 models 以外的路径。
413 request_too_large 请求体超过 16 MiB(大图请传 URL 而不是 base64)。
429 concurrency_limit 你的账户同时进行的请求过多(16 个)。按 retry-after 秒后重试。
502 upstream_error 模型不可用。请重试。
503 not_configured 服务器未启用该接口,或没有为模型配置价格。

流式输出开始后如果中断,结尾会是一个 {"error": {…}} chunk,而不是 [DONE]:模型返回了错误时沿用它的 type 和 code,否则为 upstream_error。