How to Use Claude Code with a Custom Base URL?

published
read time
3 min

If you want to use Claude Code with a custom API endpoint instead of going directly through Anthropic, this guide will walk you through the setup using:

  • Provider: ModelStack\
  • Base URL: https://api.modelstack.cc\
  • Model: claude-sonnet-4-6

This setup is useful if you:

  • Want unified billing across multiple AI providers\
  • Need proxy routing\
  • Want better regional performance\
  • Prefer centralized API management

Why Use a Custom Base URL?

Instead of calling Anthropic directly, you route your requests through:

code
https://api.modelstack.cc

ModelStack acts as a provider gateway that forwards your request to Claude while handling authentication, logging, routing, and billing.

Architecture flow:

code
Your App → api.modelstack.cc → Claude (Anthropic)

Step 1: Get Your ModelStack API Key

  1. Sign up at ModelStack\
  2. Generate an API key\
  3. Store it securely (never hardcode in production)

Example:

bash
export MODELSTACK_API_KEY="your_api_key_here"

Step 2: Install Anthropic SDK (Node.js Example)

Even though you're using ModelStack, you can still use the official Anthropic SDK --- just override the base URL.

bash
npm install @anthropic-ai/sdk

Step 3: Configure Custom Base URL

Here's how to connect Claude Sonnet 4.6 through ModelStack:

javascript
import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
  apiKey: process.env.MODELSTACK_API_KEY,
  baseURL: "https://api.modelstack.cc"
});

async function run() {
  const response = await anthropic.messages.create({
    model: "claude-sonnet-4-6",
    max_tokens: 1000,
    messages: [
      {
        role: "user",
        content: "Explain how custom base URLs work in AI APIs."
      }
    ]
  });

  console.log(response.content);
}

run();

That's it. You're now calling Claude Sonnet 4.6 via ModelStack instead of directly through Anthropic.


Step 4: cURL Example

If you prefer raw HTTP:

bash
curl https://api.modelstack.cc/v1/messages   -H "Content-Type: application/json"   -H "x-api-key: $MODELSTACK_API_KEY"   -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 500,
    "messages": [
      {"role": "user", "content": "Write a short blog intro about AI gateways."}
    ]
  }'

Understanding the Model: claude-sonnet-4-6

claude-sonnet-4-6 is a Claude Sonnet generation optimized for:

  • High-quality reasoning\
  • Code generation\
  • Long context handling\
  • Balanced cost/performance

It's a strong choice for:

  • SaaS backends\
  • Developer tools\
  • AI coding assistants\
  • Blog/content generation

Production Best Practices

Use Environment Variables

Never hardcode API keys.

Add Timeout Handling

javascript
const anthropic = new Anthropic({
  apiKey: process.env.MODELSTACK_API_KEY,
  baseURL: "https://api.modelstack.cc",
  timeout: 30000
});

Implement Retry Logic

Network layers can fail --- add exponential backoff.

Log Request IDs

If ModelStack provides request IDs, log them for debugging.


When Should You Use ModelStack?

Using a gateway like ModelStack makes sense when:

  • You want to switch providers without changing app logic\
  • You use multiple models (Claude, GPT, etc.)\
  • You need centralized analytics\
  • You operate in restricted network environments

If you're building AI-powered SaaS products, this approach gives you long-term flexibility.


Final Thoughts

Using Claude via ModelStack is simple:

  1. Install Anthropic SDK\
  2. Override baseURL to https://api.modelstack.cc\
  3. Use model claude-sonnet-4-6\
  4. Send requests as usual

That's it --- your application now communicates with Claude through a custom provider endpoint.

If you're building developer tools, AI SaaS, or automation systems, this setup gives you flexibility without sacrificing Claude's powerful capabilities.