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

vLLM: Phục Vụ LLM Hiệu Suất Cao Cho Production

vLLM: Phục Vụ LLM Hiệu Suất Cao Cho Production Trong thế giới AI hiện đại, việc triển khai mô hình ngôn ngữ lớn (LLM) vào production đặt ra nhiều thách…

Xem thêm

Gemini 2.5: Mô Hình Thinking AI Vượt Trội Reasoning Và Đa Phương Thức

Gemini 2.5 là thế hệ model AI mới nhất từ Google DeepMind — mô hình “thinking model” (mô hình tư duy) kết hợp khả năng suy luận chuỗi-of-thought (chain-of-thought) với…

Xem thêm
Banner Kimi K2 mô hình MoE 1T tham số của Moonshot AI

Kimi K2: Mô hình AI đa phương thức mã nguồn mở của Moonshot AI

Kimi K2 là mô hình ngôn ngữ lớn (LLM) mã nguồn mở của Moonshot AI, được phát hành với kiến trúc Mixture-of-Experts (MoE) quy mô 1 nghìn tỷ (1T) tham…

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