AICredits logo
Integrations

Anthropic Agent SDK

Build tool-use agents with Claude using the Anthropic SDK. Combine with AICredits for billing, monitoring, and multi-model support.

Use this page with an AI assistant

Opens a new chat with this docs URL and the correct AICredits base URLs.

The native Anthropic SDK gives you access to Claude-specific features like extended thinking and fine-grained cache control. For most agent use cases, the OpenAI-compatible AICredits endpoint is simpler and lets you switch models freely.

Overview

You have two ways to use Claude with AICredits:

ApproachWhen to useBase URL
AICredits OpenAI endpointMost agent use cases — tool use, streaming, RAGhttps://api.aicredits.in/v1
Native Anthropic SDK via AICreditsExtended thinking, explicit cache_controlhttps://api.aicredits.in (no /v1)

When to Use the Native SDK

Use the native Anthropic SDK only when you need Claude-exclusive features not available through the OpenAI-compatible layer:

  • Extended thinking / deep reasoning (budget_tokens)
  • Explicit cache_control markers for prompt caching
  • Vision with fine-grained image block control
  • Computer use (beta)

For everything else — tool use, structured output, streaming, RAG — use the AICredits OpenAI-compatible endpoint with Claude model IDs.

Tool Use Loop (Native SDK)

import json
from anthropic import Anthropic

# base_url without /v1 — the Anthropic SDK appends /v1/messages internally
client = Anthropic(
    base_url="https://api.aicredits.in",
    api_key="sk-your-aicredits-key-here",
)

tools = [
    {
        "name": "search_docs",
        "description": "Search the documentation for information",
        "input_schema": {
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "Search query"},
            },
            "required": ["query"],
        },
    }
]

def search_docs(query: str) -> str:
    return f"Results for '{query}': AICredits supports 300+ models..."

messages = [{"role": "user", "content": "What models does AICredits support?"}]

while True:
    response = client.messages.create(
        model="anthropic/claude-sonnet-4.5",
        max_tokens=1024,
        tools=tools,
        messages=messages,
    )

    messages.append({"role": "assistant", "content": response.content})

    if response.stop_reason == "end_turn":
        for block in response.content:
            if block.type == "text":
                print(block.text)
        break

    if response.stop_reason == "tool_use":
        tool_results = []
        for block in response.content:
            if block.type == "tool_use":
                result = search_docs(**block.input)
                tool_results.append({
                    "type": "tool_result",
                    "tool_use_id": block.id,
                    "content": result,
                })

        messages.append({"role": "user", "content": tool_results})

Claude via AICredits (OpenAI SDK)

For the majority of Claude agent use cases, the OpenAI SDK + AICredits endpoint is simpler and gives you unified billing across all providers:

import json
from openai import OpenAI

client = OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

tools = [{
    "type": "function",
    "function": {
        "name": "search_docs",
        "description": "Search the documentation for information",
        "parameters": {
            "type": "object",
            "properties": {
                "query": {"type": "string"},
            },
            "required": ["query"],
        },
    },
}]

messages = [{"role": "user", "content": "What models does AICredits support?"}]

while True:
    response = client.chat.completions.create(
        model="anthropic/claude-sonnet-4.5",
        messages=messages,
        tools=tools,
        tool_choice="auto",
    )

    message = response.choices[0].message
    messages.append(message)

    if response.choices[0].finish_reason == "stop":
        print(message.content)
        break

    if message.tool_calls:
        for tool_call in message.tool_calls:
            args = json.loads(tool_call.function.arguments)
            result = "AICredits supports 300+ models from OpenAI, Anthropic, Google..."
            messages.append({
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": result,
            })

Hybrid Approach

Use the native SDK for thinking features and AICredits for everything else:

from anthropic import Anthropic
from openai import OpenAI

# Anthropic SDK: base_url WITHOUT /v1 — SDK appends /v1/messages internally
anthropic_client = Anthropic(
    base_url="https://api.aicredits.in",
    api_key="sk-your-aicredits-key-here",
)

# OpenAI SDK: base_url WITH /v1 — SDK appends /chat/completions
aicredits_client = OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-aicredits-key-here",
)

def plan_with_thinking(task: str) -> str:
    """Use extended thinking to plan the approach."""
    response = anthropic_client.messages.create(
        model="anthropic/claude-sonnet-4.5",
        max_tokens=8000,
        thinking={"type": "enabled", "budget_tokens": 5000},
        messages=[{"role": "user", "content": f"Plan how to: {task}"}],
    )
    return next(b.text for b in response.content if b.type == "text")

def execute_plan(plan: str, task: str) -> str:
    """Execute the plan through AICredits (billed to your wallet)."""
    response = aicredits_client.chat.completions.create(
        model="anthropic/claude-sonnet-4.5",
        messages=[
            {"role": "system", "content": f"Execute this plan: {plan}"},
            {"role": "user", "content": task},
        ],
    )
    return response.choices[0].message.content

Base URL reminder: The Anthropic SDK uses https://api.aicredits.in (no /v1) — the SDK appends /v1/messages internally. The OpenAI SDK uses https://api.aicredits.in/v1 (with /v1) — it appends /chat/completions. Both route all billing through your AICredits INR wallet.

On this page