You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{query}from"@anthropic-ai/claude-agent-sdk";asyncfunctionmain(){forawait(constmessageofquery({prompt: "Explain what this repository does",options: {cwd: "/path/to/project",allowedTools: ["Read","Glob","Grep"],},})){if("result"inmessage){console.log(message.result);}}}main();
import{query}from"@anthropic-ai/claude-agent-sdk";forawait(constmessageofquery({prompt: "Use the code-reviewer agent to review this codebase",options: {allowedTools: ["Read","Glob","Grep","Agent"],agents: {"code-reviewer": {description: "Expert code reviewer for quality and security reviews.",prompt: "Analyze code quality and suggest improvements.",tools: ["Read","Glob","Grep"],},},},})){if("result"inmessage)console.log(message.result);}
MCP Server Integration
Browser Automation (Playwright)
forawait(constmessageofquery({prompt: "Open example.com and describe what you see",options: {mcpServers: {playwright: {command: "npx",args: ["@playwright/mcp@latest"]},},},})){if("result"inmessage)console.log(message.result);}
Session Resumption
import{query}from"@anthropic-ai/claude-agent-sdk";letsessionId: string|undefined;// First query: capture the session IDforawait(constmessageofquery({prompt: "Read the authentication module",options: {allowedTools: ["Read","Glob"]},})){if(message.type==="system"&&message.subtype==="init"){sessionId=message.session_id;}}// Resume with full context from the first queryforawait(constmessageofquery({prompt: "Now find all places that call it",options: {resume: sessionId},})){if("result"inmessage)console.log(message.result);}
Session History
import{listSessions,getSessionMessages,getSessionInfo}from"@anthropic-ai/claude-agent-sdk";asyncfunctionmain(){// List past sessions (supports pagination via limit/offset)constsessions=awaitlistSessions();for(constsessionofsessions){console.log(`Session ${session.sessionId} in ${session.cwd} (tag: ${session.tag})`);}// Get metadata for a single sessionif(sessions.length>0){constinfo=awaitgetSessionInfo(sessions[0].sessionId);console.log(`Created: ${info.createdAt}, Tag: ${info.tag}`);}// Retrieve messages from the most recent sessionif(sessions.length>0){constmessages=awaitgetSessionMessages(sessions[0].sessionId,{limit: 50});for(constmsgofmessages){console.log(msg);}}}main();
Session Mutations
import{renameSession,tagSession,forkSession}from"@anthropic-ai/claude-agent-sdk";asyncfunctionmain(){constsessionId="your-session-id";// Rename a sessionawaitrenameSession(sessionId,"Refactoring auth module");// Tag a session for filteringawaittagSession(sessionId,"experiment-v2");// Clear a tagawaittagSession(sessionId,null);// Fork a conversation to branch from a pointconst{sessionId: forkedId}=awaitforkSession(sessionId);console.log(`Forked session: ${forkedId}`);}main();
Custom System Prompt
import{query}from"@anthropic-ai/claude-agent-sdk";forawait(constmessageofquery({prompt: "Review this code",options: {allowedTools: ["Read","Glob","Grep"],systemPrompt: `You are a senior code reviewer focused on:1. Security vulnerabilities2. Performance issues3. Code maintainabilityAlways provide specific line numbers and suggestions for improvement.`,},})){if("result"inmessage)console.log(message.result);}