メインコンテンツへスキップ

Documentation Index

Fetch the complete documentation index at: https://docs.aihubmix.com/llms.txt

Use this file to discover all available pages before exploring further.

OpenAI 互換インターフェースを Claude シリーズモデル向けに、より深い最適化でアップグレードしました。Thinking とキャッシュをこれまで以上に正確かつ便利に制御できるようになりました — 特にマルチターン会話における interleaved thinking をよりユーザーフレンドリーにし、追加パラメータなしでシームレスに統合できるようになりました。さらに、Anthropic が提供する beta 機能の有効化もサポートしています。

1. モデル Thinking(Extended Thinking)

1.1 Interleaved Thinking の利点

interleaved thinking が有効でない場合、モデルは assistant ターンの最初に一度だけ thinking を実行します。それ以降のレスポンスは、新しい thinking ブロックを生成することなくツール結果を受け取った後に直接生成されます:
User → [Thinking] → Tool Call → Tool Result → Response
interleaved thinking が有効な場合、モデルはツール結果を受け取るたびに新しい thinking ブロックを挿入し、推論チェーンを形成します:
User → [Thinking] → Tool Call → Tool Result → [Thinking] → Response
                                                ↑ Interleaved Thinking
これによりモデルは以下が可能になります:
  • ツール結果に基づく二次推論を実行、単に出力を連結するだけではない。
  • 複数のツール呼び出し間で推論を連鎖、各決定は前ステップの分析に基づく。
参考:Anthropic Interleaved Thinking

1.2 Thinking の有効化

Thinking は 4 つの方法で有効化でき、いずれか 1 つを選択します:
方法説明
reasoning_effort"reasoning_effort": "low"OpenAI 標準パラメータ。リクエストボディの最上位レベルに配置
reasoning.effort"reasoning": {"effort": "low"}上記と同等。reasoning オブジェクト内に配置
reasoning.max_tokens"reasoning": {"max_tokens": 1024}thinking の最大トークン数を正確に制御
-think 付きモデル名"model": "claude-sonnet-4-5-think"最もシンプルな方法。追加パラメータ不要
優先順位(複数の方法を使用した場合):reasoning_effort > reasoning.max_tokens > reasoning.effort > -think サフィックス
effort の取りうる値: minimal / low / medium / high / xhigh

1.3 Thinking の戻り値

レスポンスメッセージには 2 つの新しいフィールドが含まれます:
  • reasoning_content:Thinking コンテンツ(文字列)。表示用。
  • reasoning_details:Thinking の完全な構造化情報。マルチターン会話ではそのまま返す必要があります。内部構造はプロバイダーごとに異なる場合があります。
非ストリーミング例(無関係なフィールドは省略):
{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "Hello! How can I help you today?",
      "reasoning_content": "The user is just saying hello...",
      "reasoning_details": {
        "type": "thinking",
        "thinking": "The user is just saying hello...",
        "signature": "Er8CCkYI..."
      }
    }
  }]
}
ストリーミングレスポンスでは、thinking コンテンツは delta.reasoning_contentdelta.reasoning_details を介してチャンクで送信されます。完全なストリーミング連結ロジックについては、以下の完全な例を参照してください。

1.4 マルチターン会話で Thinking を保持する (Interleaved Thinking は組み込みで、追加パラメータは不要)

モデルがマルチターン会話で推論能力を継続できるようにするには、前回返された reasoning_detailsそのまま 次のラウンドの assistant メッセージに配置するだけです:
messages = [
    {"role": "user", "content": "What's the weather like in Boston?"},
    {
        "role": "assistant",
        "content": response.choices[0].message.content,
        "tool_calls": response.choices[0].message.tool_calls,
        "reasoning_details": response.choices[0].message.reasoning_details,
    },
    {
        "role": "tool",
        "tool_call_id": "toolu_xxx",
        "content": '{"temperature": 45, "condition": "rainy"}',
    }
]
AihubMix はリクエスト内に履歴の thinking 情報を検出すると 自動的に interleaved thinking を有効化 し、追加のパラメータを必要とせずにツール呼び出し結果を受け取った後もモデルが深い推論を継続できるようにします。

1.5 完全な例

以下の 2 つの例は、完全なマルチターン Tool Call + interleaved thinking プロセスを示しています:ユーザー問い合わせ → モデルが思考しツールを呼び出す → ツール結果を注入(reasoning_details を保持)→ モデルの interleaved thinking が最終レスポンスを返す。 非ストリーミング・Interleaved Thinking
import os
import json
from openai import OpenAI

client = OpenAI(
    base_url="https://aihubmix.com/v1",
    api_key=os.environ.get("AIHUBMIX_API_KEY", "sk-***"),
)

# ── Tool definition ───────────────────────────────────────────
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "parameters": {
            "type": "object",
            "properties": {"location": {"type": "string", "description": "City name"}},
            "required": ["location"]
        }
    }
}]

# ── Mock tool execution ───────────────────────────────────────
WEATHER_DB = {
    "boston": {"temperature": "45°F (7°C)", "condition": "rainy", "humidity": "85%", "wind": "15 mph NE"},
    "tokyo":  {"temperature": "72°F (22°C)", "condition": "sunny", "humidity": "45%", "wind": "5 mph S"},
}

def execute_tool(name: str, args: dict) -> str:
    if name == "get_weather":
        key = next((k for k in WEATHER_DB if k in args.get("location", "").lower()), None)
        return json.dumps(WEATHER_DB.get(key, {"temperature": "65°F", "condition": "clear"}))
    return "{}"

# ── Multi-turn conversation loop ─────────────────────────────
messages = [
    {"role": "user", "content": "What's the weather like in Boston? Then recommend what to wear."}
]

turn = 0
while True:
    turn += 1
    print(f"\n── Turn {turn} ──")

    response = client.chat.completions.create(
        model="claude-sonnet-4-5",
        messages=messages,
        tools=tools,
        extra_body={"reasoning": {"max_tokens": 2000}},
    )
    msg = response.choices[0].message

    # Print thinking process
    if msg.reasoning_content:
        label = "Interleaved Thinking" if turn > 1 else "Thinking"
        print(f"[{label}] {msg.reasoning_content}")

    # Print response content
    if msg.content:
        print(f"[Response] {msg.content}")

    # Print tool calls
    if msg.tool_calls:
        for tc in msg.tool_calls:
            print(f"[Tool Call: {tc.function.name}] {tc.function.arguments}")

    # Build assistant message, preserve reasoning_details (critical!)
    assistant_msg = {"role": "assistant", "content": msg.content}
    if msg.tool_calls:
        assistant_msg["tool_calls"] = msg.tool_calls
    if msg.reasoning_details:
        assistant_msg["reasoning_details"] = msg.reasoning_details  # pass back unmodified
    messages.append(assistant_msg)

    # No tool_calls means conversation is done
    if not msg.tool_calls:
        break

    # Execute tools and append results to messages
    for tc in msg.tool_calls:
        args = json.loads(tc.function.arguments)
        result = execute_tool(tc.function.name, args)
        print(f"[Tool Result: {tc.function.name}] {result}")
        messages.append({"role": "tool", "tool_call_id": tc.id, "content": result})
ストリーミング・Interleaved Thinking
import os
import sys
import json
from openai import OpenAI

client = OpenAI(
    base_url="https://aihubmix.com/v1",
    api_key=os.environ.get("AIHUBMIX_API_KEY", "sk-***"),
)

# ── Tool definition & mock execution ─────────────────────────
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "parameters": {
            "type": "object",
            "properties": {"location": {"type": "string", "description": "City name"}},
            "required": ["location"]
        }
    }
}]

WEATHER_DB = {
    "boston": {"temperature": "45°F (7°C)", "condition": "rainy", "humidity": "85%", "wind": "15 mph NE"},
    "tokyo":  {"temperature": "72°F (22°C)", "condition": "sunny", "humidity": "45%", "wind": "5 mph S"},
}

def execute_tool(name: str, args: dict) -> str:
    if name == "get_weather":
        key = next((k for k in WEATHER_DB if k in args.get("location", "").lower()), None)
        return json.dumps(WEATHER_DB.get(key, {"temperature": "65°F", "condition": "clear"}))
    return "{}"

# ── Stream response collector ────────────────────────────────
def stream_and_collect(turn: int, **kwargs):
    """Stream response, print thinking/content in real-time, accumulate reasoning_details/tool_calls."""
    rd = {}            # accumulated reasoning_details
    content = ""       # accumulated response text
    tc_map = {}        # accumulated tool_calls (by index)
    cur = "none"       # current output section: none / thinking / content

    stream = client.chat.completions.create(stream=True, **kwargs)
    for chunk in stream:
        if not chunk.choices:
            continue
        delta = chunk.choices[0].delta

        # ── Handle thinking ──
        rd_delta = getattr(delta, "reasoning_details", None)
        if rd_delta and isinstance(rd_delta, dict):
            for k, v in rd_delta.items():
                if k == "type":
                    rd[k] = v
                elif isinstance(v, str):
                    rd[k] = rd.get(k, "") + v
                elif v is not None:
                    rd[k] = v
            # Print thinking chunks in real-time
            thinking_chunk = rd_delta.get("thinking", "")
            if thinking_chunk:
                if cur != "thinking":
                    cur = "thinking"
                    label = "Interleaved Thinking" if turn > 1 else "Thinking"
                    sys.stdout.write(f"\n[{label}] ")
                sys.stdout.write(thinking_chunk)
                sys.stdout.flush()

        # ── Handle content ──
        if delta.content:
            if cur != "content":
                if cur == "thinking":
                    sys.stdout.write("\n")
                cur = "content"
                sys.stdout.write("\n[Response] ")
            sys.stdout.write(delta.content)
            sys.stdout.flush()
            content += delta.content

        # ── Handle tool_calls ──
        for tc in delta.tool_calls or []:
            i = tc.index
            if i not in tc_map:
                tc_map[i] = {"id": "", "type": "function",
                             "function": {"name": "", "arguments": ""}}
            if tc.id:
                tc_map[i]["id"] = tc.id
            if tc.function:
                tc_map[i]["function"]["name"] += tc.function.name or ""
                tc_map[i]["function"]["arguments"] += tc.function.arguments or ""

    # End current output section
    if cur in ("thinking", "content"):
        sys.stdout.write("\n")

    tool_calls = [tc_map[i] for i in sorted(tc_map)] if tc_map else None
    return {
        "content": content or None,
        "reasoning_details": rd or None,
        "tool_calls": tool_calls,
    }

# ── Multi-turn conversation loop ─────────────────────────────
messages = [
    {"role": "user", "content": "What's the weather like in Boston? Then recommend what to wear."}
]

turn = 0
while True:
    turn += 1
    print(f"\n── Turn {turn} ──")

    result = stream_and_collect(
        turn,
        model="claude-sonnet-4-5",
        messages=messages,
        tools=tools,
        extra_body={"reasoning": {"max_tokens": 2000}},
    )

    # Print tool calls
    if result["tool_calls"]:
        for tc in result["tool_calls"]:
            print(f"[Tool Call: {tc['function']['name']}] {tc['function']['arguments']}")

    # Build assistant message, preserve reasoning_details (critical!)
    assistant_msg = {"role": "assistant", "content": result["content"]}
    if result["tool_calls"]:
        assistant_msg["tool_calls"] = result["tool_calls"]
    if result["reasoning_details"]:
        assistant_msg["reasoning_details"] = result["reasoning_details"]  # pass back unmodified
    messages.append(assistant_msg)

    # No tool_calls means conversation is done
    if not result["tool_calls"]:
        break

    # Execute tools and append results to messages
    for tc in result["tool_calls"]:
        args = json.loads(tc["function"]["arguments"])
        tool_result = execute_tool(tc["function"]["name"], args)
        print(f"[Tool Result: {tc['function']['name']}] {tool_result}")
        messages.append({"role": "tool", "tool_call_id": tc["id"], "content": tool_result})

1.6 Thinking 強度マッピングルール

Effort モード:
  • Opus 4.6 / Sonnet 4.6 以降:Anthropic ネイティブの Adaptive Thinking の effort レベルにマッピングされます。
  • その他のモデル:budget_tokens の式で計算されます:
budget_tokens = max(min(max_tokens × effort_ratio, 128000), 1024)
efforteffort_ratio
xhigh0.95
high0.80
medium0.50
low0.20
minimal0.10
Adaptive Thinking Effort マッピング:
入力 EffortOpus 4.6Sonnet 4.6
xhighmaxhigh
highhighhigh
mediummediummedium
lowlowlow
minimallowlow
max_tokens モード: Anthropic の budget_tokens として直接割り当てられます。 -think サフィックス: Opus/Sonnet 4.6+ は adaptive thinking(effort=medium)を使用。その他のモデルは budget_tokens = min(10240, max_tokens - 1) に設定され、デフォルトの max_tokens は 4096 です。

2. Prompt Caching

Chat インターフェース経由で Claude モデルにリクエストを行う際に Prompt Caching を使用できます。メッセージに cache_control ブレークポイントを設定することで、大きなテキストブロック(ロールカード、RAG データ、書籍の章など)を再利用のためにキャッシュでき、後続のリクエストでキャッシュに直接ヒットしてコストを大幅に削減できます。
Claude 公式ドキュメント:Prompt Caching

2.1 キャッシュコスト

操作価格倍率(元の入力価格に対して)
Cache Write(5 分 TTL)1.25x
Cache Write(1 時間 TTL)2x
Cache Read0.1x

2.2 サポートされるモデルと最小キャッシュ長

モデル最小キャッシュトークン数
Claude Opus 4.6 / Opus 4.54096
Claude Sonnet 4.6 / Sonnet 4.5 / Opus 4.1 / Opus 4 / Sonnet 4 / Sonnet 3.7(廃止予定)1024
Claude Haiku 4.54096
Claude Haiku 3.5(廃止予定) / Haiku 32048
ブレークポイント数制限: リクエストあたり最大 4 つの cache_control ブレークポイント。

2.3 キャッシュ TTL

TTL構文適用シナリオ
5 分(デフォルト)"cache_control": {"type": "ephemeral"}短いセッション、定常的なリクエスト
1 時間"cache_control": {"type": "ephemeral", "ttl": "1h"}長いセッション、繰り返しキャッシュ書き込みを回避
1 時間 TTL の書き込みコストは高くなりますが、長いセッションでの繰り返し書き込みを減らすことで合計費用を節約できます。Claude 4.5 以降のすべてのプロバイダー(Anthropic、Amazon Bedrock、Google Vertex AI を含む)のモデルが 1 時間 TTL をサポートします。

2.4 使い方

systemuser(画像を含む)、toolscache_control フィールドを使ってキャッシュブレークポイントを設定できます。以下の例は主要な構造のみを示しており、大きなテキストブロックは省略しています。 System メッセージのキャッシュ(デフォルト 5 分 TTL):
{
  "model": "claude-opus-4-5",
  "messages": [
    {
      "role": "system",
      "content": [
        {"type": "text", "text": "You are an AI assistant"},
        {
          "type": "text",
          "text": "(long context)",
          "cache_control": {"type": "ephemeral"}
        }
      ]
    },
    {
      "role": "user",
      "content": [{"type": "text", "text": "Hello"}]
    }
  ]
}
User メッセージのキャッシュ(1 時間 TTL):
{
  "model": "claude-opus-4-5",
  "messages": [
    {
      "role": "system",
      "content": [{"type": "text", "text": "You are an AI assistant"}]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "(long context)",
          "cache_control": {"type": "ephemeral", "ttl": "1h"}
        },
        {"type": "text", "text": "Hello"}
      ]
    }
  ]
}
画像メッセージのキャッシュ:
{
  "role": "user",
  "content": [
    {
      "type": "image_url",
      "image_url": {"detail": "auto", "url": "data:image/jpeg;base64,/9j/4AAQ..."},
      "cache_control": {"type": "ephemeral"}
    },
    {"type": "text", "text": "What's this?"}
  ]
}
Tool 定義のキャッシュ: cache_control はツールオブジェクトの最上位レベル(typefunction と並列)に配置します:
{
  "tools": [{
    "type": "function",
    "function": {
      "name": "get_weather",
      "description": "Get current weather for a location",
      "parameters": {
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"]
      }
    },
    "cache_control": {"type": "ephemeral", "ttl": "1h"}
  }]
}

2.5 キャッシュステータスの確認

レスポンスの usage には claude_cache_tokens_details が返され、詳細なキャッシュ情報が記録されます: 最初のリクエスト(キャッシュ作成):
{
  "usage": {
    "prompt_tokens": 22,
    "completion_tokens": 890,
    "total_tokens": 912,
    "claude_cache_tokens_details": {
      "cache_creation_input_tokens": 6266,
      "cache_read_input_tokens": 0,
      "cache_write_5_minutes_input_tokens": 6266,
      "cache_write_1_hour_input_tokens": 0
    }
  }
}
後続のリクエスト(キャッシュヒット):
{
  "usage": {
    "prompt_tokens": 22,
    "completion_tokens": 810,
    "total_tokens": 832,
    "prompt_tokens_details": {
      "cached_tokens": 6266
    },
    "claude_cache_tokens_details": {
      "cache_creation_input_tokens": 0,
      "cache_read_input_tokens": 6266,
      "cache_write_5_minutes_input_tokens": 0,
      "cache_write_1_hour_input_tokens": 0
    }
  }
}
フィールド意味
cache_creation_input_tokensこのリクエストでキャッシュに書き込まれたトークン数
cache_read_input_tokensこのリクエストでキャッシュから読み込まれたトークン数
cache_write_5_minutes_input_tokens5 分 TTL キャッシュに書き込まれたトークン数
cache_write_1_hour_input_tokens1 時間 TTL キャッシュに書き込まれたトークン数
prompt_tokens_details.cached_tokensキャッシュヒット時のキャッシュトークン数。OpenAI 形式と互換性あり

3. anthropic-beta 用リクエストヘッダー

HTTP ヘッダー anthropic-beta を介して Claude モデルの beta 機能を有効化でき、AihubMix が Anthropic API にスルーパスします。

使い方

リクエストヘッダーに anthropic-beta を追加し、値には対応する beta 機能識別子を指定します:
curl "https://aihubmix.com/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-***" \
  -H "anthropic-beta: context-1m-2025-08-07" \
  -d '{
  "model": "claude-opus-4-5",
  "messages": [
    {
      "role": "system",
      "content": [
        {"type": "text", "text": "You are an AI assistant"},
        {
          "type": "text",
          "text": "(long context)",
          "cache_control": {"type": "ephemeral"}
        }
      ]
    },
    {"role": "user", "content": [{"type": "text", "text": "hello"}]}
  ]
}'
利用可能な具体的な beta 識別子については、Anthropic API ドキュメント を参照してください。

最終更新日:2026-06-01