OpenAI-compatible.
Explicit by design.
Newton’s implements a focused, text-only Chat Completions surface with pinned model IDs, prepaid balances, streaming, and USD request receipts. It does not claim compatibility with every OpenAI or Anthropic endpoint.
List, select, run.
Install the OpenAI Python SDK, keep your key in the environment, then select an exact ID from the live catalog. Do not hard-code a model name that the catalog did not return.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://newtons.dev/api/v1",
api_key=os.environ["NEWTONS_API_KEY"],
)
catalog = client.models.list()
if not catalog.data:
raise RuntimeError("No models published.")
model_id = catalog.data[0].id
completion = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": "Reply exactly: Newton's connected"}],
max_tokens=32,
)
print(completion.choices[0].message.content)
print("resolved model:", completion.model)Expected application result: a normal Chat Completion whose model matches the selected published ID. The JSON response also contains a newtons receipt object. Keep the key out of source code, chat, logs, screenshots, and committed files.
{
"id": "chatcmpl-…",
"object": "chat.completion",
"model": "<published-model-id>",
"choices": [{ "index": 0, "message": { "role": "assistant", "content": "Newton's connected" } }],
"usage": { "prompt_tokens": 12, "completion_tokens": 4, "total_tokens": 16 },
"newtons": {
"request_id": "…",
"requested_model": "<published-model-id>",
"resolved_model": "<published-model-id>",
"fallback_used": false,
"cost_usd": "…",
"balance_usd": "…",
"cost_estimated": false
}
}Give your coding agent the job.
Paste this into Codex, Claude Code, Cursor, Copilot, or another repo-aware agent. The prompt fails closed when the key or published catalog is unavailable.
Onboard this repository to Newton’s model API.
Security rules:
- Never ask me to paste, echo, or reveal the API key in chat, source code, command arguments, logs, or committed files.
- Read the key only from the NEWTONS_API_KEY process environment variable.
- If NEWTONS_API_KEY is missing, stop and tell me how to set it in my local shell or secret manager without asking for its value.
- Ensure .env and .env.local are ignored by git. Keep .env.example safe to commit and free of real secrets.
Implementation:
1. Use the project’s existing OpenAI SDK. Set base_url to https://newtons.dev/api/v1 and api_key to NEWTONS_API_KEY from the environment.
2. Call client.models.list() and choose only an exact model ID returned by the published catalog. If the list is empty, stop and report: “No models published.”
3. Make one non-streaming chat completion smoke test with max_tokens=16 and the user message: “Reply exactly: Newton’s connected”.
4. Confirm the requested model matches the resolved model. Report the request ID and USD cost headers when the SDK exposes them.
5. Keep the change minimal. Report the files changed, selected model ID, and smoke-test result.
Do not invent a model ID, enable an undisclosed fallback, print the key, or commit a secret.Focused surface.
Use https://newtons.dev/api/v1 as the SDK base URL. All routes require Authorization: Bearer $NEWTONS_API_KEY.
/modelsList exact model IDs currently published to your account.
/pricingRead USD input, cached-input, output, reasoning, tool, and fixed-request rates.
/balanceRead available, reserved, promotional, and purchased service-credit balances.
/chat/completionsCreate non-streaming or streaming, text-only Chat Completions.
Read through the final receipt.
Set stream=True and consume the stream until [DONE]. Newton’s appends a terminal chunk with empty choices and the settled newtons receipt before the done sentinel.
stream = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": "Explain inertia in one sentence."}],
max_tokens=80,
stream=True,
)
receipt = None
for chunk in stream:
if chunk.choices:
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
extra = getattr(chunk, "model_extra", None) or {}
receipt = extra.get("newtons") or receipt
print("\\nreceipt:", receipt)The HTTP receipt headers at stream start are an authorization estimate. The terminal receipt is the settled result. If the stream emits an error event, treat it as terminal and do not execute tool side effects from an incomplete response.
Retry by class, not by instinct.
| Status | Typical type | What to do |
|---|---|---|
| 400 | invalid_request_error | Fix the JSON, message shape, text-only content, or output-token value. Do not retry unchanged. |
| 401 | authentication_error | Check that the environment contains an active newt_… key. Never log the key. |
| 402 | insufficient_balance | Reduce max_tokens or add service credit. Do not automatically retry. |
| 403 | account_locked | Stop traffic and contact support. Do not rotate through accounts or keys. |
| 404 | model_not_found | Refresh GET /models and select a currently published ID. |
| 413 | invalid_request_error | Reduce the serialized request below 128 KB. |
| 429 | rate_limit_error | Honor Retry-After, then retry with jitter. Reduce parallelism if concurrency is the cause. |
| 502 | route_unavailable or identity error | Retry a transient route error with bounded backoff. Do not loop on a model-identity error; refresh the catalog or contact support. |
| 503 | route, capacity, price, or wallet unavailable | Check status and the live catalog. Retry a small number of times with exponential backoff; stop if the condition persists. |
At most four total attempts for 429, transient 502, or 503: honor Retry-After when present, otherwise wait approximately 1s, 2s, then 4s with random jitter. Cap the delay and total deadline in your own application.
A retry can create a second billable completion if the first request finished upstream but its response was lost. Retry only operations your application can safely repeat, and never retry a completed tool side effect merely because its reporting request failed.
Known envelopes.
Promotional access
Promotional concurrency
Funded access
Funded concurrency
Maximum output tokens
Maximum request body
Capacity, safety, fraud, balance, and model-availability controls also apply. A rate or concurrency response uses 429 and includes Retry-After.
Every accepted call accounts for itself.
x-newtons-request-cost-usd
x-newtons-balance-usd
x-newtons-cost-estimated
x-newtons-request-id
x-newtons-requested-model
x-newtons-resolved-model
x-newtons-fallback-used
x-newtons-rpm-limit
x-newtons-concurrency-limitUsage is reserved before inference, reconciled against returned usage, and settled in integer micro-USD internally. Receipts are accounting metadata, not cryptographic attestations. Save the request ID—not the prompt or API key—when asking for support.
Pinned means no silent swap.
A published model ID remains pinned to that route. Newton’s requires the upstream-reported ID to match; mismatched or unconfirmed responses fail closed. If you explicitly provide documented fallback_models, Newton’s may try up to three of those published IDs in the order supplied and reports whether a fallback was used.
Automatic model routing is not currently part of the public API.
Know what is—and is not—implemented.
| Capability | Support | Notes |
|---|---|---|
| OpenAI Chat Completions | Supported | Text-only, n=1, non-streaming and streaming. |
| Model, pricing, and balance lists | Supported | Authenticated Newton’s routes under /api/v1. |
| Tool-call payloads | Route-dependent | Passed through on published models that accept the supplied tool schema. |
| Explicit fallback models | Supported | Newton’s extension; up to three published IDs. Never implicit. |
| Images, audio, video, and files | Not supported | The launch Chat Completions surface accepts text content only. |
| OpenAI Responses, Assistants, embeddings, batches, fine-tuning | Not supported | Use only the documented routes above. |
| Anthropic Messages API | Not supported | Newton’s does not currently expose a native /messages adapter. |
| Automatic model routing | Not supported | Select a catalog ID or provide explicit fallback IDs. |
Service incident? Check newtons.dev/status before retrying.
Account, billing, or model mismatch? Email founders@newtons.dev with the request ID, UTC timestamp, endpoint, and error type. Never send your API key or full prompt.
Security issue? Follow security.txt and include a minimal reproduction without customer data.
NEWTON’S