API docs
Guides

Use the Anthropic SDK and Claude Code

Point the Anthropic SDK or Claude Code at a Nucleus-served model with a base URL change.

Nucleus speaks the Anthropic wire format at https://api.nucleus-ai.io/anthropic. The routes accept Anthropic's native x-api-key header, so an unmodified Anthropic SDK, and Claude Code itself, work with only a base URL change. Authorization: Bearer works too; if both headers are present, Authorization wins. Requests to these routes need a key with the infer scope.

This guide covers SDK setup, running Claude Code against your model, messages, streaming, the thinking controls and token counting. For the full parameter tables see the Anthropic-compatible reference.

Point the SDK at Nucleus

Pass the Nucleus base URL and your key when you construct the client. The SDK sends the key as x-api-key, which Nucleus accepts as is.

import anthropic

client = anthropic.Anthropic(
    base_url='https://api.nucleus-ai.io/anthropic',
    api_key=API_KEY,  # nk_live_...
)

The model parameter accepts either a base model ID, such as Qwen/Qwen3-8B, or the nucleus:// path of your fine-tuned sampler weights, such as nucleus://run_3Bz:train:0/sampler_weights/000240. A finished fine-tuning job reports this path as result_model.

Run Claude Code against your model

Claude Code reads its endpoint and key from the environment, so two exports point it at a Nucleus-served model. Set ANTHROPIC_MODEL to choose which model it drives:

export ANTHROPIC_BASE_URL=https://api.nucleus-ai.io/anthropic
export ANTHROPIC_API_KEY=nk_live_8xQ2...
export ANTHROPIC_MODEL='nucleus://run_3Bz:train:0/sampler_weights/000240'
claude

Claude Code leans heavily on tool use, which the messages route supports (tools and tool_choice pass through unchanged).

Send a message

Messages work exactly as they do against Anthropic: model, max_tokens and messages are required, system sets the system prompt, and temperature, top_p, top_k and stop_sequences control sampling.

resp = client.messages.create(
    model='nucleus://run_3Bz:train:0/sampler_weights/000240',
    max_tokens=1024,
    system='You are a support assistant for Acme.',
    messages=[{'role': 'user', 'content': 'What is 17 x 23?'}],
)
print(resp.content[-1].text)

The response is a standard Anthropic message. By default the model thinks before it answers, so content carries a thinking block ahead of the text block:

{
  "type": "message",
  "role": "assistant",
  "content": [
    { "type": "thinking", "thinking": "17 × 23..." },
    { "type": "text", "text": "391" }
  ],
  "stop_reason": "end_turn"
}

Streaming

Set stream to true and the response arrives as Anthropic-format server-sent events, so the SDKs' streaming helpers work unchanged.

with client.messages.stream(
    model='nucleus://run_3Bz:train:0/sampler_weights/000240',
    max_tokens=1024,
    messages=[{'role': 'user', 'content': 'Write a haiku about GPUs.'}],
) as stream:
    for text in stream.text_stream:
        print(text, end='', flush=True)

Thinking and effort

Two controls shape the model's reasoning. To turn thinking off entirely, pass thinking with type disabled; the response then contains only text blocks. To tune how hard the model thinks, set output_config.effort to a level from low up to max. The Python SDK passes fields it does not know about via extra_body.

resp = client.messages.create(
    model='nucleus://run_3Bz:train:0/sampler_weights/000240',
    max_tokens=1024,
    messages=[{'role': 'user', 'content': 'What is 17 x 23?'}],
    thinking={'type': 'disabled'},
)

resp = client.messages.create(
    model='nucleus://run_3Bz:train:0/sampler_weights/000240',
    max_tokens=1024,
    messages=[{'role': 'user', 'content': 'Prove it step by step.'}],
    extra_body={'output_config': {'effort': 'max'}},
)

Count tokens before you send

Count tokens returns the exact input token count for a request, using the model's real tokenizer and renderer rather than an estimate. system and tools are counted when present. Use it to quote the cost of a request before running it.

count = client.messages.count_tokens(
    model='nucleus://run_3Bz:train:0/sampler_weights/000240',
    system='You are a support assistant for Acme.',
    messages=[{'role': 'user', 'content': 'How do I reset my API key?'}],
)
print(count.input_tokens)  # 34

Limitations

Prompt caching and citations are not supported on the Anthropic-compatible routes. Everything shown above, including tool use, streaming and token counting, behaves as an Anthropic client expects.

If you use the OpenAI SDK instead, see Use the OpenAI SDK.

Use the Anthropic SDK and Claude Code | Nucleus AI API