V2インターフェースは不安定なプレビューです。安定版になる前にフィードバックに基づいてAPIが変更される可能性があります。セッションフォーキングなどの一部の機能はV1 SDKでのみ利用可能です。
V2 Claude Agent TypeScript SDKは、非同期ジェネレータとyield調整の必要性を排除します。これにより、マルチターン会話がより簡単になります。ターン間でジェネレータの状態を管理する代わりに、各ターンは個別のsend()/receive()サイクルです。APIサーフェスは3つの概念に削減されます:
createSession() / resumeSession(): 会話を開始または継続するsession.send(): メッセージを送信するsession.receive(): レスポンスを取得するV2インターフェースは既存のSDKパッケージに含まれています:
npm install @anthropic-ai/claude-agent-sdkセッションを維持する必要がない単純なシングルターンクエリの場合は、unstable_v2_prompt()を使用します。この例は数学の質問を送信し、答えをログに出力します:
import { unstable_v2_prompt } from '@anthropic-ai/claude-agent-sdk'
const result = await unstable_v2_prompt('What is 2 + 2?', {
model: 'claude-sonnet-4-5-20250929'
})
console.log(result.result)import { query } from '@anthropic-ai/claude-agent-sdk'
const q = query({
prompt: 'What is 2 + 2?',
options: { model: 'claude-sonnet-4-5-20250929' }
})
for await (const msg of q) {
if (msg.type === 'result') {
console.log(msg.result)
}
}単一のプロンプトを超えるインタラクションの場合は、セッションを作成します。V2は送受信を異なるステップに分離します:
send()はメッセージをディスパッチしますreceive()はレスポンスをストリーミングで返しますこの明示的な分離により、ターン間にロジックを追加しやすくなります(レスポンスを処理してからフォローアップを送信するなど)。
以下の例はセッションを作成し、「Hello!」をClaudeに送信し、テキストレスポンスを出力します。await using(TypeScript 5.2+)を使用して、ブロックが終了するときにセッションを自動的に閉じます。session.close()を手動で呼び出すこともできます。
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
await using session = unstable_v2_createSession({
model: 'claude-sonnet-4-5-20250929'
})
await session.send('Hello!')
for await (const msg of session.receive()) {
// Filter for assistant messages to get human-readable output
if (msg.type === 'assistant') {
const text = msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
console.log(text)
}
}V1では、入出力は単一の非同期ジェネレータを通じてフローします。基本的なプロンプトの場合は同様に見えますが、マルチターンロジックを追加するには、入力ジェネレータを使用するように再構成する必要があります。
import { query } from '@anthropic-ai/claude-agent-sdk'
const q = query({
prompt: 'Hello!',
options: { model: 'claude-sonnet-4-5-20250929' }
})
for await (const msg of q) {
if (msg.type === 'assistant') {
const text = msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
console.log(text)
}
}セッションは複数の交換にわたってコンテキストを保持します。会話を続けるには、同じセッションでsend()を再度呼び出します。Claudeは前のターンを覚えています。
この例は数学の質問を尋ね、その後、前の答えを参照するフォローアップを尋ねます:
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
await using session = unstable_v2_createSession({
model: 'claude-sonnet-4-5-20250929'
})
// Turn 1
await session.send('What is 5 + 3?')
for await (const msg of session.receive()) {
// Filter for assistant messages to get human-readable output
if (msg.type === 'assistant') {
const text = msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
console.log(text)
}
}
// Turn 2
await session.send('Multiply that by 2')
for await (const msg of session.receive()) {
if (msg.type === 'assistant') {
const text = msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
console.log(text)
}
}import { query } from '@anthropic-ai/claude-agent-sdk'
// Must create an async iterable to feed messages
async function* createInputStream() {
yield {
type: 'user',
session_id: '',
message: { role: 'user', content: [{ type: 'text', text: 'What is 5 + 3?' }] },
parent_tool_use_id: null
}
// Must coordinate when to yield next message
yield {
type: 'user',
session_id: '',
message: { role: 'user', content: [{ type: 'text', text: 'Multiply by 2' }] },
parent_tool_use_id: null
}
}
const q = query({
prompt: createInputStream(),
options: { model: 'claude-sonnet-4-5-20250929' }
})
for await (const msg of q) {
if (msg.type === 'assistant') {
const text = msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
console.log(text)
}
}前のインタラクションからセッションIDがある場合は、後でそれを再開できます。これは長時間実行されるワークフローや、アプリケーションの再起動全体で会話を永続化する必要がある場合に便利です。
この例はセッションを作成し、そのIDを保存し、閉じてから会話を再開します:
import {
unstable_v2_createSession,
unstable_v2_resumeSession,
type SDKMessage
} from '@anthropic-ai/claude-agent-sdk'
// Helper to extract text from assistant messages
function getAssistantText(msg: SDKMessage): string | null {
if (msg.type !== 'assistant') return null
return msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
}
// Create initial session and have a conversation
const session = unstable_v2_createSession({
model: 'claude-sonnet-4-5-20250929'
})
await session.send('Remember this number: 42')
// Get the session ID from any received message
let sessionId: string | undefined
for await (const msg of session.receive()) {
sessionId = msg.session_id
const text = getAssistantText(msg)
if (text) console.log('Initial response:', text)
}
console.log('Session ID:', sessionId)
session.close()
// Later: resume the session using the stored ID
await using resumedSession = unstable_v2_resumeSession(sessionId!, {
model: 'claude-sonnet-4-5-20250929'
})
await resumedSession.send('What number did I ask you to remember?')
for await (const msg of resumedSession.receive()) {
const text = getAssistantText(msg)
if (text) console.log('Resumed response:', text)
}import { query } from '@anthropic-ai/claude-agent-sdk'
// Create initial session
const initialQuery = query({
prompt: 'Remember this number: 42',
options: { model: 'claude-sonnet-4-5-20250929' }
})
// Get session ID from any message
let sessionId: string | undefined
for await (const msg of initialQuery) {
sessionId = msg.session_id
if (msg.type === 'assistant') {
const text = msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
console.log('Initial response:', text)
}
}
console.log('Session ID:', sessionId)
// Later: resume the session
const resumedQuery = query({
prompt: 'What number did I ask you to remember?',
options: {
model: 'claude-sonnet-4-5-20250929',
resume: sessionId
}
})
for await (const msg of resumedQuery) {
if (msg.type === 'assistant') {
const text = msg.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
console.log('Resumed response:', text)
}
}セッションは手動で、またはawait usingを使用して自動的に閉じることができます。これはTypeScript 5.2+の自動リソースクリーンアップ機能です。古いTypeScriptバージョンを使用しているか、互換性の問題が発生した場合は、代わりに手動クリーンアップを使用してください。
自動クリーンアップ(TypeScript 5.2+):
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
await using session = unstable_v2_createSession({
model: 'claude-sonnet-4-5-20250929'
})
// Session closes automatically when the block exits手動クリーンアップ:
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
const session = unstable_v2_createSession({
model: 'claude-sonnet-4-5-20250929'
})
// ... use the session ...
session.close()unstable_v2_createSession()マルチターン会話用の新しいセッションを作成します。
function unstable_v2_createSession(options: {
model: string;
// Additional options supported
}): Sessionunstable_v2_resumeSession()IDで既存のセッションを再開します。
function unstable_v2_resumeSession(
sessionId: string,
options: {
model: string;
// Additional options supported
}
): Sessionunstable_v2_prompt()シングルターンクエリ用のワンショット便利関数。
function unstable_v2_prompt(
prompt: string,
options: {
model: string;
// Additional options supported
}
): Promise<Result>interface Session {
send(message: string): Promise<void>;
receive(): AsyncGenerator<SDKMessage>;
close(): void;
}すべてのV1機能がV2でまだ利用可能なわけではありません。以下はV1 SDKを使用する必要があります:
forkSessionオプション)V2インターフェースが安定版になる前に、フィードバックを共有してください。GitHub Issuesを通じて問題と提案を報告してください。