Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Claude Agent SDKは、エージェントと対話するための2つの異なる入力モードをサポートしています:
このガイドでは、各モードの違い、利点、ユースケースについて説明し、アプリケーションに適したアプローチを選択するのに役立てます。
ストリーミング入力モードは、Claude Agent SDKを使用する推奨される方法です。エージェントの機能に完全にアクセスでき、豊かでインタラクティブな体験を実現します。
これにより、エージェントは長期的なプロセスとして動作し、ユーザー入力を受け取り、割り込みを処理し、権限リクエストを表示し、セッション管理を処理できます。
メッセージに画像を直接添付して、ビジュアル分析と理解を実現
複数のメッセージを順序立てて処理し、割り込む機能を備えて送信
セッション中のすべてのツールとカスタムMCPサーバーへの完全なアクセス
ライフサイクルフックを使用して、様々なポイントで動作をカスタマイズ
最終結果だけでなく、生成されるレスポンスをリアルタイムで確認
複数のターンにわたって自然に会話コンテキストを維持
シングルメッセージ入力はより単純ですが、より制限されています。
シングルメッセージ入力を使用する場合:
シングルメッセージ入力モードは以下をサポートしていません:
import { query } from "@anthropic-ai/claude-agent-sdk";
import { readFileSync } from "fs";
async function* generateMessages() {
// First message
yield {
type: "user" as const,
message: {
role: "user" as const,
content: "Analyze this codebase for security issues"
}
};
// Wait for conditions or user input
await new Promise(resolve => setTimeout(resolve, 2000));
// Follow-up with image
yield {
type: "user" as const,
message: {
role: "user" as const,
content: [
{
type: "text",
text: "Review this architecture diagram"
},
{
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: readFileSync("diagram.png", "base64")
}
}
]
}
};
}
// Process streaming responses
for await (const message of query({
prompt: generateMessages(),
options: {
maxTurns: 10,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
// Simple one-shot query
for await (const message of query({
prompt: "Explain the authentication flow",
options: {
maxTurns: 1,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
}
// Continue conversation with session management
for await (const message of query({
prompt: "Now explain the authorization process",
options: {
continue: true,
maxTurns: 1
}
})) {
if (message.type === "result") {
console.log(message.result);
}
}