English

Connect Anthropic SDK

Connect Anthropic SDK to ModelPort using Messages.

Setup

  1. Install the Python anthropic package and set MODELPORT_API_KEY.
  2. Set MODELPORT_TEXT_MODEL to a text model available to your account. The SDK appends /v1/messages to the configured domain.

Anthropic SDK configuration

import os
from uuid import uuid4
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["MODELPORT_API_KEY"], base_url="https://api.apimodel.store")
message = client.messages.create(
    model=os.environ["MODELPORT_TEXT_MODEL"],
    messages=[{"role": "user", "content": "Reply with one short sentence."}],
    max_tokens=128,
    extra_headers={"Idempotency-Key": str(uuid4())},
)
for block in message.content:
    if block.type == "text":
        print(block.text)

Verify the connection

Check the model catalog for a text model with available: true. A text route must be open to your account before you can send chat requests.

Text API and billing

Anthropic SDK official documentation ↗