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 tool – claude 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.md và CONTRIBUTING.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

Self-attention là gì: cơ chế Q, K, V trong mô hình ngôn ngữ

Self-attention là gì: Q, K, V và cách mô hình ngôn ngữ tính ngữ cảnh Self-attention là cơ chế cho phép mô hình ngôn ngữ tự tính mối quan hệ…

Xem thêm

RAG là gì: retrieval-augmented generation và cách hoạt động

RAG là gì: retrieval-augmented generation và cách hoạt động Retrieval-augmented generation (RAG) là một kỹ thuật tiên tiến trong lĩnh vực AI giúp mô hình ngôn ngữ lớn (LLM) truy…

Xem thêm

Prompt engineering là gì: kỹ thuật viết lệnh cho AI hiệu quả

Prompt engineering là gì: kỹ thuật viết lệnh cho AI Prompt engineering là kỹ thuật thiết kế và tinh chỉnh câu lệnh (prompt) gửi cho mô hình AI để đạt…

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