Skip to main content

รัน Claude Code แบบ Programmatic

ใช้ Agent SDK เพื่อรัน Claude Code แบบ programmatic จาก CLI, Python หรือ TypeScript

Agent SDK มอบเครื่องมือ agent loop และการจัดการ context เดียวกับที่ขับเคลื่อน Claude Code พร้อมใช้เป็น CLI สำหรับ script และ CI/CD หรือเป็นแพ็คเกจ Python และ TypeScript สำหรับการควบคุมแบบ programmatic เต็มรูปแบบ

ในการรัน Claude Code ใน non-interactive mode ส่ง -p พร้อม prompt และ CLI options ใด ๆ:

claude -p "Find and fix the bug in auth.py" --allowedTools "Read,Edit,Bash"

การใช้งานพื้นฐาน

เพิ่ม flag -p (หรือ --print) กับคำสั่ง claude ใด ๆ เพื่อรันแบบ non-interactive

claude -p "What does the auth module do?"

เริ่มต้นเร็วขึ้นด้วย Bare Mode

เพิ่ม --bare เพื่อลดเวลาเริ่มต้นโดยข้ามการค้นหาอัตโนมัติของ hooks, skills, plugins, MCP servers, auto memory และ CLAUDE.md Bare mode มีประโยชน์สำหรับ CI และ script ที่ต้องการผลลัพธ์เดิมทุกครั้ง

claude --bare -p "Summarize this file" --allowedTools "Read"

ใน bare mode Claude สามารถเข้าถึงเครื่องมือ Bash, อ่านไฟล์ และแก้ไขไฟล์ ส่ง context ที่ต้องการด้วย flag:

ต้องการโหลดใช้
System prompt additions--append-system-prompt, --append-system-prompt-file
Settings--settings <file-or-json>
MCP servers--mcp-config <file-or-json>
Custom agents--agents <json>
Plugin--plugin-dir <path>, --plugin-url <url>

หมายเหตุ: --bare คือโหมดที่แนะนำสำหรับการเรียกแบบ script และ SDK และจะเป็นค่าเริ่มต้นสำหรับ -p ในเวอร์ชันอนาคต

ตัวอย่าง

Pipe ข้อมูลผ่าน Claude

cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt

หมายเหตุ: stdin ที่ pipe มาถูกจำกัดที่ 10MB ตั้งแต่ Claude Code v2.1.128

เพิ่ม Claude ใน Build Script

{
"scripts": {
"lint:claude": "git diff main | claude -p \"you are a typo linter. for each typo in this diff, report filename:line on one line and the issue on the next. return nothing else.\""
}
}

รับ Structured Output

ใช้ --output-format เพื่อควบคุมการส่งคืนผลลัพธ์:

  • text (ค่าเริ่มต้น): output ข้อความธรรมดา
  • json: JSON ที่มีโครงสร้างพร้อม result, session ID และ metadata
  • stream-json: JSON ที่คั่นด้วย newline สำหรับ streaming แบบ real-time
claude -p "Summarize this project" --output-format json

ดึง output ด้วย jq:

# ดึง text result
claude -p "Summarize this project" --output-format json | jq -r '.result'

# ดึง structured output
claude -p "Extract function names from auth.py" \
--output-format json \
--json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}' \
| jq '.structured_output'

Stream ผลลัพธ์

claude -p "Explain recursion" --output-format stream-json --verbose --include-partial-messages

Auto-approve Tools

claude -p "Run the test suite and fix any failures" \
--allowedTools "Bash,Read,Edit"

ตั้งค่า permission mode พื้นฐาน:

claude -p "Apply the lint fixes" --permission-mode acceptEdits

สร้าง Commit

claude -p "Look at my staged changes and create an appropriate commit" \
--allowedTools "Bash(git diff *),Bash(git log *),Bash(git status *),Bash(git commit *)"

กำหนด System Prompt เอง

gh pr diff "$1" | claude -p \
--append-system-prompt "You are a security engineer. Review for vulnerabilities." \
--output-format json

ต่อ Conversation

# Request แรก
claude -p "Review this codebase for performance issues"

# ต่อ conversation ล่าสุด
claude -p "Now focus on the database queries" --continue
claude -p "Generate a summary of all issues found" --continue

บันทึก session ID เพื่อ resume เซสชันเฉพาะ:

session_id=$(claude -p "Start a review" --output-format json | jq -r '.session_id')
claude -p "Continue that review" --resume "$session_id"

ขั้นตอนถัดไป

  • Agent SDK quickstart: สร้าง agent แรกของคุณด้วย Python หรือ TypeScript
  • CLI reference: flag และ option ทั้งหมดของ CLI
  • GitHub Actions: ใช้ Agent SDK ใน GitHub workflows
  • GitLab CI/CD: ใช้ Agent SDK ใน GitLab pipeline