Skip to content
AI Growth Lab
Navigation menu
AI Growth Lab

Loading lesson…

CoursesContactPrivacy

Curriculum based on Datawhale / Easy Vibe · CC BY-NC-SA 4.0

← Back to course

AI-Native Engineer

Claude Code Fundamentals
  1. Getting Started with Claude Code
  2. Essential Claude Code Techniques
  3. Configuring Claude Code for Your Project
  4. Everyday Development Workflows
  5. Troubleshooting Claude Code
MCP: Connecting Claude Code to Tools
  1. Getting Started with MCP in Claude Code
  2. Using and Troubleshooting MCP
  3. Configuring Common MCP Servers
Claude Code Skills
  1. Finding and Using Your First Skills
  2. Understanding Skills and SKILL.md
  3. Creating and Improving Your Own Skills
  4. How Claude Skills Work Internally
Claude Agent Teams
  1. Understanding and Setting Up Agent Teams
  2. Build an RPG with Agent Teams
  3. Agent Teams Best Practices
  4. Choosing When to Use Agent Teams
AI-Assisted Development Workflows
  1. Choosing an AI Development Strategy
  2. Developing Features and Fixing Bugs with AI
  3. Refactoring and Reviewing Code with AI
  4. Building a Project Knowledge Base
  5. Improving AI Collaboration Efficiency
Building Agents with Claude Agent SDK
  1. Getting Started with Claude Agent SDK
  2. Advanced Features and Practical Agent Examples
  3. Building a Code-Quality Guardrail Pipeline
Engineering Workflows with Superpowers
  1. Getting Started with Superpowers and Its Core Skills
  2. Build an Authentication System with Superpowers
  3. Configuring and Using Superpowers Effectively
Spec Coding
  1. Spec Coding Foundations
  2. Practicing Spec Coding in Claude Code
  3. Moving from Vibe Coding to Spec Coding
  4. Spec Coding FAQ and Summary
GitHub Spec-Driven Development
  1. Workflow Foundations and Setup
  2. From Product Idea to Specification
  3. From Specification to Working Software
  4. Reusable Prompts and Responsible Autonomy
Cross-Platform Development
  1. From a Web Page to Real Software and an AI-Native Product
  2. How to Choose the Right Platform for Your App
  3. How to Build an Android App with Jetpack Compose
  4. How to Build an iOS App with SwiftUI
  5. Build a Store Inspection App with React Native and Expo
On this page
  • Enterprise Practice: Building a Code-Quality Guardrail Pipeline
  • Error Handling

Lesson Contents

  1. Enterprise Practice: Building a Code-Quality Guardrail Pipeline
  2. Error Handling
  3. Summary
  4. References
AI Development FAQ and Summary
How to Build a Cross-Platform App with Flutter
  • How to Build a PWA Local App
  • How to Build a Browser AI Assistant Extension
  • How to Build a Cross-Platform Electron Desktop App
  • How to Build Platform, Pixel-Art, and 3D Games with Godot
  • How to Build a VS Code Extension
  • How to Build an Industrial Qt Desktop App
  • How to Build Your Personal Website and Academic Blog
  • How to Publish the Application You Built
  • Summary
  • References
  • Enterprise Practice: Building a Code-Quality Guardrail Pipeline

    The previous scenarios all used one agent for one job. In real enterprise environments, what you need is a full pipeline - multiple agents chained together, each stage with clear input/output, plus auditing, rollback, and notifications.

    Now we will build a real scenario: after each PR submission, automatically trigger code review -> security scan -> auto-fix -> test verification -> report generation as a complete pipeline.

    Architecture Design

    PR submitted
      │
      ▼
    ┌─────────────┐    ┌─────────────┐    ┌─────────────┐
    │  Code Review │───▶│ Security Scan│───▶│   Auto Fix   │
    │    Agent     │    │    Agent     │    │    Agent     │
    │ (read-only)  │    │ (read-only)  │    │ (writable)   │
    └─────────────┘    └─────────────┘    └─────────────┘
                                                │
                                                ▼
                                         ┌─────────────┐    ┌─────────────┐
                                         │ Test Verify  │───▶│ Report Build │
                                         │    Agent     │    │    Agent     │
                                         │   (Bash)     │    │   (Write)    │
                                         └─────────────┘    └─────────────┘
                                                                  │
                                                                  ▼
                                                           Slack notification
    

    Core idea: each agent does one thing, permissions are minimized, and results are passed in sequence.

    Step 1: Define the Pipeline Framework

    import asyncio
    import json
    from datetime import datetime
    from claude_agent_sdk import query, ClaudeAgentOptions, HookMatcher
    
    # Audit log: record every operation by every agent
    audit_log = []
    
    async def audit_hook(input_data, tool_use_id, context):
        audit_log.append({
            "time": datetime.now().isoformat(),
            "tool": input_data.get("tool_name"),
            "input": input_data.get("tool_input", {}),
        })
        return {}
    
    # Shared hook config: all agents share audit capability
    audit_hooks = {
        "PostToolUse": [HookMatcher(matcher=".*", hooks=[audit_hook])]
    }
    

    Step 2: Code Review Agent (Read-Only)

    async def run_code_review(pr_diff: str) -> str:
        """Read-only agent, reviews code quality and outputs a structured report"""
        result_text = ""
        async for message in query(
            prompt=f"""Review the following PR diff from these dimensions:
    1. Code conventions: naming, formatting, comments
    2. Logic issues: edge cases, null pointer risks, race conditions
    3. Performance risks: N+1 queries, memory leaks, unnecessary loops
    4. Maintainability: oversized functions, unclear responsibilities, magic numbers
    
    PR Diff:
    {pr_diff}
    
    Output JSON format: {{"issues": [{{"severity": "high/medium/low", "file": "...", "line": ..., "description": "..."}}], "summary": "..."}}""",
            options=ClaudeAgentOptions(
                allowed_tools=["Read", "Glob", "Grep"],
                permission_mode="bypassPermissions",
                hooks=audit_hooks,
                max_turns=10,
            ),
        ):
            if hasattr(message, "result"):
                result_text = message.result
        return result_text
    

    Step 3: Security Scan Agent (Read-Only)

    async def run_security_scan() -> str:
        """Read-only agent focused on vulnerability scanning"""
        result_text = ""
        async for message in query(
            prompt="""Scan the project code for security vulnerabilities:
    1. SQL injection, XSS, CSRF
    2. Hardcoded keys or credentials
    3. Insecure dependency versions
    4. Missing permission checks
    
    Output JSON: {{"vulnerabilities": [{{"severity": "critical/high/medium", "type": "...", "file": "...", "description": "...", "fix_suggestion": "..."}}]}}""",
            options=ClaudeAgentOptions(
                allowed_tools=["Read", "Glob", "Grep", "Bash"],
                permission_mode="bypassPermissions",
                hooks=audit_hooks,
                max_turns=15,
            ),
        ):
            if hasattr(message, "result"):
                result_text = message.result
        return result_text
    

    Step 4: Auto-Fix Agent (Writable)

    async def run_auto_fix(review_result: str, security_result: str) -> str:
        """Writable agent that auto-fixes code based on review and scan results"""
        result_text = ""
        async for message in query(
            prompt=f"""Fix code according to the following review results:
    
    Code review report:
    {review_result}
    
    Security scan report:
    {security_result}
    
    Fix rules:
    1. Only fix issues with severity high or critical
    2. Run related tests after each change to ensure no existing functionality is broken
    3. Do not refactor unrelated code, apply minimal fixes only
    4. Output the list of modified files after completion""",
            options=ClaudeAgentOptions(
                allowed_tools=["Read", "Edit", "Bash", "Glob", "Grep"],
                permission_mode="acceptEdits",
                hooks=audit_hooks,
                max_turns=30,
            ),
        ):
            if hasattr(message, "result"):
                result_text = message.result
        return result_text
    

    Step 5: Test Verification + Report Generation

    async def run_test_and_report(fix_result: str) -> str:
        """Run tests and generate final report"""
        result_text = ""
        async for message in query(
            prompt=f"""Execute these actions:
    1. Run the full test suite (npm test or pytest)
    2. Compute test pass rate
    3. Generate a Markdown quality report into pr-report.md, including:
       - Count of issues found in code review and severity distribution
       - Number of security vulnerabilities
       - Auto-fix changes: {fix_result}
       - Test pass rate
       - Final conclusion: whether merge is recommended""",
            options=ClaudeAgentOptions(
                allowed_tools=["Read", "Bash", "Write", "Glob"],
                hooks=audit_hooks,
                max_turns=15,
            ),
        ):
            if hasattr(message, "result"):
                result_text = message.result
        return result_text
    

    Step 6: Chain the Whole Pipeline

    import subprocess
    
    async def run_pipeline():
        """Full PR quality-guard pipeline"""
        print("🔍 Stage 1/4: code review...")
        pr_diff = subprocess.run(
            ["git", "diff", "main...HEAD"], capture_output=True, text=True
        ).stdout
        review_result = await run_code_review(pr_diff)
    
        print("🛡️ Stage 2/4: security scan...")
        security_result = await run_security_scan()
    
        print("🔧 Stage 3/4: auto-fix...")
        fix_result = await run_auto_fix(review_result, security_result)
    
        print("✅ Stage 4/4: test verification + report generation...")
        report = await run_test_and_report(fix_result)
    
        # Save audit log
        with open("audit-log.json", "w") as f:
            json.dump(audit_log, f, indent=2, ensure_ascii=False)
    
        print(f"Pipeline finished, audit log saved ({len(audit_log)} operation records)")
        return report
    
    asyncio.run(run_pipeline())
    

    Enterprise Design Thinking

    This pipeline reflects several key enterprise design principles:

    Least privilege: code-review and security-scan agents are read-only and cannot accidentally modify code. Only the auto-fix agent has write permission, and even that is constrained by acceptEdits.

    Auditable: every step of every agent is logged through Hooks. If anything goes wrong, you can trace which agent did what and when.

    Result chaining: each agent's output becomes the next agent's input. Review results feed auto-fix; auto-fix results feed test verification. Every stage has a clear input/output contract.

    Cost control: every agent has a max_turns limit to prevent runaway loops. In production, you can also add max_budget_usd for budget control.

    Extensibility: want another stage, such as a "documentation-check agent" or "performance benchmark agent"? Add a new function and insert it into the pipeline.

    This model can be embedded directly into GitHub Actions or GitLab CI, automatically triggered on each PR, truly achieving "AI-driven code quality guardrails."


    Error Handling

    Agent SDK provides clear exception types so you can build robust fault tolerance in production:

    from claude_agent_sdk import query, CLINotFoundError, ProcessError
    
    try:
        async for msg in query(prompt="Analyze code"):
            print(msg)
    except CLINotFoundError:
        print("Claude Code CLI is not installed. Please install it first.")
    except ProcessError as e:
        print(f"Process exited unexpectedly with exit code: {e.exit_code}")
    

    Summary

    The core value of Claude Agent SDK is upgrading "model reasoning" into "controlled execution." It does not just generate text. It can truly complete tasks inside an auditable, constrained tool system.

    Remember a line from Anthropic's official blog: the Agent SDK design philosophy is "give the agent a computer and let it work like a human."

    A good agent application = clear tool design + explicit task boundaries + appropriate human supervision. Tools give the agent capability, boundaries give it constraints, and supervision gives you confidence. None of the three can be missing.


    References

    Official Resources

    • Agent SDK Official Docs - the most authoritative reference
    • GitHub - claude-agent-sdk-python - Python SDK source
    • GitHub - claude-agent-sdk-typescript - TypeScript SDK source
    • Agent SDK Demo Projects - email assistant, research agent, and more

    Blogs and Tutorials

    • Building agents with the Claude Agent SDK - Anthropic engineering blog on design philosophy and architecture
    • Claude Agent SDK Python Study Guide - Chinese-friendly full tutorial from zero
    • Claude Agent SDK Full Tutorial - practical guide to tool systems, Agent Loop, and controlled execution
    • 12 Practical Agent SDK Scenarios - covers coding, data, automation, and more
    • Step-by-Step Agent Tutorial - TypeScript + Python dual-track tutorial
    Previous
    Sign in to track progress
    Next