Claude Code SDK: Tự động hóa coding với Anthropic API

Claude Code SDK là bộ công cụ chính thức từ Anthropic, cho phép lập trình viên tích hợp Claude trực tiếp vào quy trình coding — từ IDE, CLI, đến CI/CD. Không chỉ là chatbot, đây là AI coding assistant có khả năng đọc codebase, chạy shell command, và tự động refactor.

Claude Code SDK là gì?

Claude Code SDK cung cấp hai interface chính:

  • CLI toolclaude command chạy trong terminal, tự động scan context cwd, git diff, và environment variables.
  • API SDK@anthropic-ai/claude-code package, dùng trong Node.js scripts để programmatically invoke Claude với tools.

Cài đặt và cấu hình nhanh

1. Install CLI

npm install -g @anthropic-ai/claude-code

# hoặc với pnpm
pnpm add -g @anthropic-ai/claude-code

# Authenticate với Anthropic API key
claude auth login
Terminal showing Claude Code CLI installation and auth command

2. Cấu hình MCP servers

Claude Code hỗ trợ MCP (Model Context Protocol) để kết nối external tools:

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/project"]
    },
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_TOKEN": "ghp_xxx"
      }
    },
    "postgres": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-postgres"],
      "env": {
        "DATABASE_URL": "postgres://user:***@localhost/db"
      }
    }
  }
}

MCP servers giúp Claude access file system, query database, hoặc tạo GitHub PR — tất cả trong cùng một session.

Sử dụng CLI trong thực tế

Interactive mode

cd /path/to/your/project
claude

Claude tự động:

  • Đọc README.md, package.json, file structure
  • Hiểu git status + diff
  • Suggest code changes theo best practices
  • Chạy npm test hoặc pytest để verify

One-shot command

claude "Refactor auth middleware to use JWT with refresh tokens"
claude --model claude-opus-4-5 "Write unit tests for utils/validation.ts"

Claude sẽ đọc file, hiểu logic hiện tại, rồi suggest patch hoặc write new code.

Claude Code terminal interface showing code refactoring suggestion

API SDK: Tích hợp vào CI/CD

import { ClaudeCode } from '@anthropic-ai/claude-code';

const claude = new ClaudeCode({
  apiKey: process.env.ANTHROPIC_API_KEY,
  model: 'claude-opus-4-5'
});

const result = await claude.run({
  prompt: 'Review this PR for security vulnerabilities',
  context: {
    files: ['src/auth.ts', 'src/middleware.ts'],
    diff: gitDiff
  },
  tools: ['read_file', 'run_command']
});

console.log(result.output);

SDK này dùng để:

  • Automated code review – Chạy trên mỗi PR, gửi feedback comment.
  • Auto-fix lint errors – Đọc ESLint output, generate fix PR.
  • Generate changelog – Phân tích git log, viết markdown changelog.

So sánh Claude Code vs Cursor vs Copilot

Tính năng Claude Code Cursor GitHub Copilot
Context window 200k tokens 128k tokens 8k tokens
Read entire codebase Yes Yes (pro) No
Run shell commands Yes Limited No
MCP support Yes No No
Git-aware Yes Yes Yes
CLI-first Yes No No

Claude Code mạnh nhất cho workflow automation và multi-step reasoning. Cursor tốt hơn cho real-time autocomplete. Copilot lightweight nhưng thiếu context dài.

Best practices

  • Luôn cung cấp README.mdCONTRIBUTING.md – Claude dùng làm context chính.
  • Dùng MCP servers thay vì hardcode API credentials.
  • Chạy claude --dry-run trước khi apply changes lớn.
  • Pin model version trong CI: --model claude-opus-4-5-20250514.

Tài liệu tham khảo

Tôi là một lập trình viên IOS. Code chính là IOS nhưng thỉnnh thoảng vẫn đá sang Android hoặc web. Mặc dù không quá thông thạo nhưng tôi sẽ chia sẻ những kiến thức mà mình đã tìm hiểu, áp dụng qua.

Bài viết liên quan

Ollama: Chạy LLM cục bộ không cần GPU đắt tiền

Bạn muốn chạy mô hình ngôn ngữ lớn (LLM) ngay trên laptop cá nhân mà không cần đầu tư card đồ họa đắt tiền? Ollama là giải pháp hoàn hảo…

Xem thêm
Sơ đồ đồ thị LangGraph với các node và edge điều khiển luồng Agent

LangGraph: Xây dựng AI Agent stateful với graph-based workflow

LangGraph là gì? LangGraph là framework của LangChain cho phép xây dựng AI Agent có trạng thái (stateful) dựa trên đồ thị (graph). Khác với Chain đơn giản thực thi…

Xem thêm

AI Observability: Giám sát, debug và tối ưu hệ thống AI production

Hệ thống AI đưa vào production khác biệt hoàn toàn so với thử nghiệm trong lab. Trong lab, bạn đo lường accuracy, loss, perplexity trên tập test cố định. Trong…

Xem thêm
0 0 đánh giá
Article Rating
Theo dõi
Thông báo của
guest
0 Comments
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất