クイック回答
LLMwithCryptoではTool callingはどのように動作しますか?
関数をtoolsにOpenAI形式で送信します。モデルが関数を必要とすると、LLMwithCryptoは関数名とJSONの引数を含むfinish_reason: "tool_calls"を返します。あなたのコードがそれを実行し、結果をtoolメッセージとして返信します。構造化された応答には、response_format: {"type": "json_object"}で有効なJSONを要求します。どちらも対応しているモデルで動作します(2026年10月23日時点のドキュメントに基づく)。
どのモデルがtoolsとJSONに対応していますか?
ストリーミングはすべてのモデルで利用できます。tool callingとJSON出力は、各モデル自体が対応している範囲に従います。
依存する前に、使用予定のモデルで独自のスキーマをテストしてください。toolsに対応していないモデルは、関数を呼び出さずにプレーンテキストで応答します。JSONモードに対応していないモデルは、response_formatに対して400エラーを返します。
ループとは何ですか?
関数を記述する
各toolには
name、モデルがいつ使うかを判断するために読むdescription、そしてJSON Schemaとしてのparametersがあります。toolsを含めて会話を送信する
モデルはテキストで応答するか、
finish_reason: "tool_calls"で停止し、message.tool_callsに1つ以上の呼び出しを含めます。各呼び出しを実行する
function.arguments(JSON文字列)をパースし、関数を実行します。引数を鵜呑みにせず、他のユーザー入力と同様に検証してください。結果を返信する
assistantメッセージを追加し、次に各呼び出しごとに
tool_call_idと結果をテキストとして含むtoolメッセージを1つ追加します。再度APIを呼び出し、モデルがテキストで応答するまで繰り返します。
import json
import os
from openai import OpenAI
client = OpenAI(
base_url="https://llmwithcrypto.com/api/v1",
api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
)
tools = [
{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Return the delivery status of an order.",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
},
},
}
]
def get_order_status(order_id):
return {"order_id": order_id, "status": "shipped", "eta": "2026-10-25"}
messages = [{"role": "user", "content": "Where is my order A-1042?"}]
while True:
resp = client.chat.completions.create(model="gpt-oss-120b", messages=messages, tools=tools, max_tokens=300)
msg = resp.choices[0].message
if not msg.tool_calls:
print(msg.content)
break
messages.append(msg)
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_order_status(**args)
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
応答ではtool呼び出しはどのように表示されますか?
assistantメッセージにはテキストがなく、tool_calls配列があり、choiceはfinish_reason: "tool_calls"で終わります。引数はオブジェクトではなくJSON文字列として渡されます。以下のIDと数値は例示です。
{
"id": "chatcmpl-3c9d27e1",
"object": "chat.completion",
"created": 1792742400,
"model": "gpt-oss-120b",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_7f31",
"type": "function",
"function": {
"name": "get_order_status",
"arguments": "{\"order_id\": \"A-1042\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 118,
"completion_tokens": 22,
"total_tokens": 140
}
}
toolsがいつ使われるかを制御するには?
"tool_choice": "auto"(toolsが設定されている場合のデフォルト): モデルが判断します。"tool_choice": "none": toolsがリストされていても、モデルはテキストで応答します。"tool_choice": "required": モデルは少なくとも1つのtoolを呼び出す必要があります。{"type": "function", "function": {"name": "get_order_status"}}: モデルはその1つを呼び出す必要があります。
各tool定義は呼び出しごとに入力として送信されるため、長い説明や多くのtoolsは入力トークンを増やします。説明は短くし、そのステップに必要なtoolsだけを送信してください。
有効なJSONを取得するには?
response_formatを{"type": "json_object"}に設定し、期待するキーをsystemメッセージで説明します。JSONモードは応答がパース可能であることを保証するもので、望むキーが含まれることを保証するものではありません。安定した出力のためにtemperatureを低く保ち、使用前にコードでパースしたオブジェクトを確認してください。
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://llmwithcrypto.com/api/v1',
apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});
const resp = await client.chat.completions.create({
model: 'gpt-oss-120b',
messages: [
{ role: 'system', content: 'Reply with a JSON object {"sentiment": "positive" | "negative" | "neutral", "reason": string}.' },
{ role: 'user', content: 'The delivery was late but support fixed it in an hour.' },
],
response_format: { type: 'json_object' },
temperature: 0.2,
max_tokens: 150,
});
const result = JSON.parse(resp.choices[0].message.content);
console.log(result.sentiment, '-', result.reason);
toolsとJSON: よくある質問
tool呼び出しには追加料金がかかりますか?
それ自体に料金はありません。tool定義、呼び出し、返信する結果はすべてトークンであり、他のトークンと同様に課金されます。残高からトークン単位で支払います。入力トークンと出力トークンはモデル自身のトークナイザーでカウントされ、そのモデルの100万トークンあたりの価格で請求されます。エラーで終わった呼び出しは無料です。
モデルは複数のtoolsを一度に呼び出せますか?
一部のモデルはtool_callsに複数の呼び出しを返します。すべてを実行し、次のリクエストの前に各呼び出しごとにtoolメッセージを1つ送信してください。
APIは私の関数を実行しますか?
いいえ。モデルはどの関数をどの引数で呼び出すかを示すだけであり、あなたのコードが実行します。当社側では何も実行されず、やり取りの内容も保存されません: プロンプトやモデルの回答は保存しません。レスポンスを送信した時点で、そのテキストは残りません。各呼び出しにつき、時刻、アカウント、モデル、トークン数を記録したログが1行残り、30日後に削除されます。