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

KV cache là gì: bộ nhớ chi phối tốc độ sinh văn bản của LLM

KV cache là gì? là bộ nhớ tạm trong quá trình suy luận mô hình ngôn ngữ lớn, dùng để lưu lại kết quả trung gian của lớp attention mỗi…

Xem thêm

Mạng nơ-ron tích chập (CNN) là gì: cách AI nhìn và đọc ảnh

Mạng nơ-ron tích chập (Convolutional Neural Network, viết tắt CNN) là kiến trúc mạng nơ-ron được thiết kế riêng cho dữ liệu có cấu trúc không gian như ảnh, video…

Xem thêm
Biểu đồ biến thiên phương sai nhiễu theo từng bước trong bộ lập lịch nhiễu tuyến tính, trục hoành là số bước từ 0 tới 1000

Mô hình khuếch tán là gì? Cách AI tạo ảnh chất lượng cao từ nhiễu

Mô hình khuếch tán là gì? Cách AI tạo ảnh chất lượng cao từ nhiễu Mô hình khuếch tán (diffusion model) là một lớp mô hình sinh tạo dữ liệu…

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