Examples
Use the Anthropic Messages API
Point the Anthropic SDK at api.redgold.ai and authenticate with a bearer token instead of x-api-key.
Goal
Call a Redgold model through the Anthropic Messages API shape on the Redgold model API.
Prerequisites
- An
sk-rg-key inREDGOLD_API_KEY(see Create an API key). pip install anthropic.- A
/v1/messages-family model id from/v1/models(see Model API).
Steps
Two details differ from a direct Anthropic call. Set base_url to the host without a /v1 suffix — the SDK appends /v1/messages itself. And present the key on the Authorization: Bearer header, because the model API reads that header, not the x-api-key the SDK sends by default.
import os
from anthropic import Anthropic
client = Anthropic(
base_url="https://api.redgold.ai",
api_key="unused", # the model API reads Authorization, not x-api-key
default_headers={"Authorization": f"Bearer {os.environ['REDGOLD_API_KEY']}"},
)
with client.messages.stream(
model="model-id-from-v1-models",
max_tokens=256,
messages=[{"role": "user", "content": "Hello"}],
) as stream:
for text in stream.text_stream:
print(text, end="")
Expected output
The assistant's reply streamed to stdout token by token.
When it breaks
401— the key reached the API only asx-api-key. Confirm theAuthorization: Bearerheader is set indefault_headersas shown.404on/v1/messages—base_urlincludes a/v1suffix. Drop it; the SDK adds the path.400unknown model — the id is not a/v1/messages-family model for your account. Read one from/v1/models.
Source
Model API (Anthropic Messages).