Skip to content

Claude Code Complete Guide

Claude Code AI Agent Hands-On Implementation: Hooks Automation and Agent Mode Practical Code Collection [2025 Edition]

Introduction

Building on the concepts of Claude Code & GitHub Copilot Agent Mode introduced in the morning article, this article focuses on actual code implementation.

We'll provide detailed explanations of practical implementation examples that can be used immediately in real projects, covering how to build AI agents capable of running continuously for 7 hours, how to achieve automation with Hooks, and more.

Key Points

  • Fully Autonomous Coding

    Complete automation from issue creation to implementation, testing, and deployment without manual intervention

  • Hooks Auto-Execution

    AI automatically processes at commit, merge, and deployment timing

  • GitHub Actions Integration

    AI workflows triggered by ${{ github.event }}

  • Multi-Agent Collaboration

    Integrated system of Claude Code + Copilot + external APIs

1. Claude Code Hooks Complete Implementation Guide

1.1 Basic Configuration File

// ~/.claude/settings.json
{
  "hooks": {
    "PreToolUse": [
      {
        "name": "Security Check",
        "condition": {"files_changed": ["*.env", "*.key", "secrets/*"]},
        "hooks": [
          {
            "type": "command",
            "command": "echo 'WARNING: Sensitive file detected' && exit 1"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "name": "Auto Test Runner",
        "condition": {"files_changed": ["src/**/*.ts", "lib/**/*.js"]},
        "hooks": [
          {
            "type": "command",
            "command": "npm test -- --passWithNoTests"
          },
          {
            "type": "command", 
            "command": "npm run lint:fix"
          }
        ]
      },
      {
        "name": "AI Code Review",
        "condition": {"tool_used": "Edit", "file_size": ">1000"},
        "hooks": [
          {
            "type": "claude_prompt",
            "prompt": "Review this code change for:\n1. Security vulnerabilities\n2. Performance issues\n3. Best practices\n4. Test coverage needs"
          }
        ]
      }
    ],
    "OnError": [
      {
        "name": "Auto Debug",
        "condition": {"error_type": "compilation"},
        "hooks": [
          {
            "type": "claude_prompt",
            "prompt": "Analyze this compilation error and suggest 3 specific fixes:\n{error_message}"
          }
        ]
      }
    ]
  },
  "ai_agent": {
    "model": "claude-opus-4",
    "max_autonomous_steps": 1000,
    "cache_duration": 3600,
    "tools": ["web-search", "file-system", "git", "npm", "docker"]
  }
}

1.2 Project-Specific CLAUDE.md Configuration

# CLAUDE.md - AI Agent Configuration

## Project Context
- **Framework**: Next.js 14 + TypeScript
- **Database**: PostgreSQL + Prisma
- **Testing**: Jest + Playwright
- **Deployment**: Vercel + GitHub Actions

## Automated Workflows

### Pre-commit Automation
```bash
# Run these before every commit
npm run type-check
npm run lint:fix  
npm test -- --coverage --threshold=80
npx prisma validate

Post-merge Automation

# Run after successful merge to main
npm install
npm run build
npm run test:e2e
npm run db:migrate:prod

Error Recovery Protocols

  1. Build failures: Auto-analyze logs and suggest fixes
  2. Test failures: Generate missing test cases
  3. Type errors: Auto-fix simple TypeScript issues
  4. Dependency conflicts: Suggest resolution strategies

AI Agent Permissions

  • ✅ Read/write source files
  • ✅ Execute npm scripts
  • ✅ Git operations (branch, commit, push)
  • ✅ Database migrations (staging only)
  • ❌ Production deployments
  • ❌ Secrets modification
  • ❌ System-level commands

Custom Commands

  • claude analyze-performance: Analyze bundle size and performance
  • claude generate-tests: Auto-generate unit tests for new functions
  • claude fix-types: Resolve TypeScript compilation errors
  • claude optimize-db: Suggest database query optimizations
    ### 1.3 Advanced Hooks Implementation Example
    
    ```typescript
    // claude-hooks-advanced.ts
    import { HookConfig, ToolUseEvent, ErrorEvent } from '@anthropic/claude-code';
    import { WebSearch } from './services/web-search';
    import { NotificationService } from './services/notifications';
    
    export class AdvancedHooksManager {
      private webSearch = new WebSearch();
      private notifications = new NotificationService();
    
      // Hook with complex conditional logic
      async onFileChanged(event: ToolUseEvent): Promise<void> {
        const { filePath, changes } = event;
    
        // Detect changes to security-critical files
        if (this.isSecurityCritical(filePath)) {
          await this.performSecurityAudit(filePath, changes);
        }
    
        // Detect API endpoint changes
        if (this.isAPIEndpoint(filePath)) {
          await this.updateAPIDocumentation(filePath);
          await this.generateAPITests(filePath);
        }
    
        // Detect database schema changes
        if (filePath.includes('schema.prisma')) {
          await this.validateMigration();
          await this.updateTypeDefinitions();
        }
      }
    
      // Automatic recovery on error
      async onError(event: ErrorEvent): Promise<string> {
        const { errorType, message, stackTrace, context } = event;
    
        switch (errorType) {
          case 'TypeScriptError':
            return await this.autoFixTypeScript(message, context);
    
          case 'TestFailure':
            return await this.analyzeTestFailure(message, stackTrace);
    
          case 'BuildError':
            return await this.resolveBuildIssue(message, context);
    
          case 'DependencyConflict':
            return await this.resolveDependencies(message);
    
          default:
            // For unknown errors, search for solutions externally
            const solution = await this.webSearch.findSolution(message);
            return `Suggested solution from web search:\n${solution}`;
        }
      }
    
      private async autoFixTypeScript(error: string, context: any): Promise<string> {
        // Auto-fix TypeScript errors
        const patterns = [
          {
            pattern: /Property '(\w+)' does not exist on type/,
            fix: (match: RegExpMatchArray) => 
              `Add property '${match[1]}' to the type definition or use optional chaining`
          },
          {
            pattern: /Argument of type '(\w+)' is not assignable to parameter of type '(\w+)'/,
            fix: (match: RegExpMatchArray) => 
              `Convert ${match[1]} to ${match[2]} or update the function signature`
          }
        ];
    
        for (const { pattern, fix } of patterns) {
          const match = error.match(pattern);
          if (match) {
            return fix(match);
          }
        }
    
        return "Manual TypeScript error resolution required";
      }
    
      private async performSecurityAudit(filePath: string, changes: string): Promise<void> {
        // Security audit implementation
        const vulnerabilities = await this.scanForVulnerabilities(changes);
    
        if (vulnerabilities.length > 0) {
          await this.notifications.sendSlack({
            channel: '#security-alerts',
            message: `🚨 Security vulnerabilities detected in ${filePath}`,
            attachments: vulnerabilities
          });
    
          throw new Error(`Security audit failed: ${vulnerabilities.join(', ')}`);
        }
      }
    
      private async updateAPIDocumentation(filePath: string): Promise<void> {
        // Automatic API documentation update
        const endpoints = await this.extractAPIEndpoints(filePath);
        await this.generateOpenAPISpec(endpoints);
        await this.updatePostmanCollection(endpoints);
      }
    
      private isSecurityCritical(filePath: string): boolean {
        const securityPatterns = [
          /auth/,
          /security/,
          /middleware.*auth/,
          /api.*admin/,
          /config.*prod/
        ];
    
        return securityPatterns.some(pattern => pattern.test(filePath));
      }
    
      private isAPIEndpoint(filePath: string): boolean {
        return filePath.includes('/api/') && filePath.endsWith('.ts');
      }
    }
    

2. GitHub Copilot Agent Mode Implementation

2.1 Copilot Agent Configuration File

# .github/copilot-workspace.yml
version: "1.0"
name: "AI Development Workspace"

agent:
  model: "claude-sonnet-4"
  capabilities:
    - code_generation
    - code_review
    - issue_resolution
    - test_generation
    - documentation

  environment:
    node_version: "20"
    package_manager: "npm"
    python_version: "3.11"

  tools:
    - name: "web-search"
      enabled: true
      api_key: ${{ secrets.SEARCH_API_KEY }}

    - name: "database"
      enabled: true
      connection: ${{ secrets.DATABASE_URL }}

    - name: "deployment"
      enabled: false  # Disabled for security

workflows:
  issue_resolution:
    trigger:
      - type: "issue_assigned"
        assignee: "@github-copilot"

    steps:
      - name: "analyze_issue"
        action: "understand_requirements"

      - name: "create_implementation_plan"
        action: "generate_technical_plan"

      - name: "create_branch"
        action: "git_create_branch"
        pattern: "copilot/issue-${{ github.event.issue.number }}"

      - name: "implement_solution"
        action: "code_generation"
        test_first: true

      - name: "run_tests"
        action: "execute_tests"
        coverage_threshold: 80

      - name: "create_pull_request"
        action: "create_pr"
        reviewers: ["team-lead", "senior-dev"]

  automated_code_review:
    trigger:
      - type: "pull_request"
        action: "opened"

    steps:
      - name: "analyze_changes"
        action: "code_analysis"
        focus: ["security", "performance", "maintainability"]

      - name: "suggest_improvements"
        action: "generate_suggestions"

      - name: "check_test_coverage"
        action: "coverage_analysis"
        fail_below: 80

  dependency_updates:
    trigger:
      - type: "schedule"
        cron: "0 2 * * 1"  # Every Monday at 2 AM

    steps:
      - name: "check_outdated"
        action: "npm_outdated"

      - name: "security_audit"
        action: "npm_audit"

      - name: "update_safe_dependencies"
        action: "auto_update"
        strategy: "patch_only"

      - name: "create_update_pr"
        action: "create_pr"
        title: "🤖 Weekly dependency updates"

2.2 VS Code Integration Configuration

// .vscode/settings.json
{
  "github.copilot.agent.enabled": true,
  "github.copilot.agent.model": "claude-sonnet-4",
  "github.copilot.agent.workspace": ".github/copilot-workspace.yml",

  "github.copilot.agent.features": {
    "multiFileEditing": true,
    "semanticSearch": true,
    "autonomousMode": true,
    "contextAwareness": true,
    "codeGeneration": true,
    "testGeneration": true
  },

  "github.copilot.agent.hooks": {
    "preCommit": [
      "npm run type-check",
      "npm run lint:fix",
      "npm test -- --passWithNoTests"
    ],
    "postMerge": [
      "npm install",
      "npm run build"
    ],
    "onError": [
      "claude-code analyze-error --context ${{ error }}"
    ]
  },

  "github.copilot.agent.permissions": {
    "allowedCommands": [
      "npm", "git", "docker", "pytest", "jest"
    ],
    "deniedPaths": [
      ".env*", "secrets/", "credentials.json"
    ],
    "maxFileSize": "1MB",
    "maxExecutionTime": "300s"
  }
}

2.3 Custom Copilot Agent Implementation

// copilot-agent-custom.ts
import { CopilotAgent, AgentConfig, TaskResult } from '@github/copilot-sdk';
import { ClaudeCode } from '@anthropic/claude-code';

export class CustomCopilotAgent extends CopilotAgent {
  private claudeCode: ClaudeCode;

  constructor(config: AgentConfig) {
    super(config);
    this.claudeCode = new ClaudeCode({
      model: 'claude-opus-4',
      apiKey: process.env.ANTHROPIC_API_KEY
    });
  }

  // Issue auto-resolution implementation
  async resolveIssue(issueNumber: string): Promise<TaskResult> {
    const issue = await this.github.getIssue(issueNumber);

    // Step 1: Requirements analysis with Claude Code
    const analysis = await this.claudeCode.analyze({
      prompt: `Analyze this GitHub issue and create implementation plan:

      Title: ${issue.title}
      Description: ${issue.body}
      Labels: ${issue.labels.join(', ')}

      Provide:
      1. Technical requirements
      2. File structure needed
      3. Implementation approach
      4. Test strategy`,

      tools: ['web-search', 'code-analysis']
    });

    // Step 2: Create branch
    const branch = `copilot/issue-${issueNumber}`;
    await this.git.createBranch(branch);

    // Step 3: Implementation
    const implementation = await this.implementSolution(analysis.plan);

    // Step 4: Test generation and execution
    const testResults = await this.generateAndRunTests(implementation);

    // Step 5: Create PR
    const pr = await this.createPullRequest({
      branch,
      title: `Resolve #${issueNumber}: ${issue.title}`,
      body: this.generatePRDescription(analysis, implementation, testResults)
    });

    return {
      success: true,
      branch,
      pullRequest: pr.number,
      implementation,
      testResults
    };
  }

  // Execute complex implementation tasks
  private async implementSolution(plan: any): Promise<any> {
    const tasks = plan.tasks;
    const results = [];

    for (const task of tasks) {
      switch (task.type) {
        case 'create_component':
          const component = await this.generateReactComponent(task);
          results.push(component);
          break;

        case 'create_api':
          const api = await this.generateAPIEndpoint(task);
          results.push(api);
          break;

        case 'update_database':
          const migration = await this.generateDatabaseMigration(task);
          results.push(migration);
          break;

        case 'add_tests':
          const tests = await this.generateTestSuite(task);
          results.push(tests);
          break;
      }
    }

    return results;
  }

  private async generateReactComponent(task: any): Promise<any> {
    const componentCode = await this.claudeCode.generate({
      prompt: `Generate a React TypeScript component with:

      Name: ${task.componentName}
      Props: ${JSON.stringify(task.props)}
      Functionality: ${task.description}

      Requirements:
      - TypeScript with proper types
      - Responsive design using Tailwind CSS
      - Accessibility (ARIA labels, keyboard navigation)
      - Error boundaries
      - Unit tests with React Testing Library`,

      format: 'typescript-react',
      includeTests: true
    });

    // Create files
    await this.writeFile(
      `src/components/${task.componentName}.tsx`,
      componentCode.component
    );

    await this.writeFile(
      `src/components/__tests__/${task.componentName}.test.tsx`,
      componentCode.tests
    );

    return {
      type: 'component',
      files: [componentCode.component, componentCode.tests],
      path: `src/components/${task.componentName}.tsx`
    };
  }

  private async generateAPIEndpoint(task: any): Promise<any> {
    const apiCode = await this.claudeCode.generate({
      prompt: `Generate a Next.js API route with:

      Endpoint: ${task.endpoint}
      Method: ${task.method}
      Parameters: ${JSON.stringify(task.parameters)}
      Response: ${JSON.stringify(task.response)}

      Requirements:
      - Input validation with Zod
      - Error handling
      - Rate limiting
      - Authentication check
      - OpenAPI documentation
      - Integration tests`,

      format: 'nextjs-api',
      includeTests: true,
      includeValidation: true
    });

    await this.writeFile(
      `pages/api/${task.endpoint}.ts`,
      apiCode.handler
    );

    await this.writeFile(
      `__tests__/api/${task.endpoint}.test.ts`,
      apiCode.tests
    );

    return {
      type: 'api',
      files: [apiCode.handler, apiCode.tests],
      path: `pages/api/${task.endpoint}.ts`
    };
  }

  // Auto-generate PR description
  private generatePRDescription(analysis: any, implementation: any, testResults: any): string {
    return `## 🤖 Auto-generated by Copilot Agent

### Summary
${analysis.summary}

### Changes Made
${implementation.map((item: any) => `- ${item.type}: ${item.path}`).join('\n')}

### Test Results
- ✅ Unit tests: ${testResults.unit.passed}/${testResults.unit.total}
- ✅ Integration tests: ${testResults.integration.passed}/${testResults.integration.total}
- ✅ Coverage: ${testResults.coverage}%

### Performance Impact
- Bundle size: ${testResults.bundleSize}
- Build time: ${testResults.buildTime}

### Security Checklist
- [x] Input validation implemented
- [x] Authentication checks added
- [x] No sensitive data exposed
- [x] Rate limiting configured

### Deployment Notes
${analysis.deploymentNotes || 'No special deployment requirements'}

---
*This PR was automatically created by GitHub Copilot Agent Mode*`;
  }
}

3. GitHub Actions Integration Workflow

3.1 AI Agent Execution Workflow

# .github/workflows/ai-agent-workflow.yml
name: AI Agent Automation

on:
  issues:
    types: [opened, assigned]
  pull_request:
    types: [opened, synchronize]
  schedule:
    - cron: '0 6 * * *'  # Every day at 6 AM
  workflow_dispatch:
    inputs:
      agent_task:
        description: 'Task for AI Agent'
        required: true
        type: choice
        options:
          - 'code_review'
          - 'dependency_update'
          - 'performance_audit'
          - 'security_scan'

jobs:
  ai-agent-orchestrator:
    runs-on: ubuntu-latest
    timeout-minutes: 60

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install Dependencies
        run: |
          npm ci
          npm install -g @anthropic/claude-code

      - name: Configure AI Agents
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # Claude Code configuration
          mkdir -p ~/.claude
          echo '{"api_key": "${{ secrets.ANTHROPIC_API_KEY }}"}' > ~/.claude/config.json

          # GitHub Copilot configuration
          gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"

      - name: Execute AI Agent Task
        id: ai-task
        env:
          GITHUB_EVENT: ${{ toJson(github.event) }}
          TASK_TYPE: ${{ github.event_name }}
        run: |
          # Execute tasks based on event type
          case "$TASK_TYPE" in
            "issues")
              if [[ "${{ github.event.action }}" == "assigned" ]] && [[ "${{ github.event.assignee.login }}" == "github-copilot" ]]; then
                echo "🤖 Executing Issue Resolution Agent"
                node scripts/ai-agent-issue-resolver.js ${{ github.event.issue.number }}
              fi
              ;;
            "pull_request")
              echo "🔍 Executing Code Review Agent"
              node scripts/ai-agent-code-reviewer.js ${{ github.event.pull_request.number }}
              ;;
            "schedule")
              echo "🔄 Executing Maintenance Agent"
              node scripts/ai-agent-maintenance.js
              ;;
            "workflow_dispatch")
              echo "⚡ Executing Custom Task: ${{ github.event.inputs.agent_task }}"
              node scripts/ai-agent-custom.js "${{ github.event.inputs.agent_task }}"
              ;;
          esac

      - name: Generate AI Report
        if: always()
        run: |
          echo "## AI Agent Execution Report" > ai-agent-report.md
          echo "- **Trigger**: ${{ github.event_name }}" >> ai-agent-report.md
          echo "- **Timestamp**: $(date -u)" >> ai-agent-report.md
          echo "- **Status**: ${{ steps.ai-task.outcome }}" >> ai-agent-report.md

          if [ -f "ai-task-output.json" ]; then
            echo "- **Results**: " >> ai-agent-report.md
            cat ai-task-output.json >> ai-agent-report.md
          fi

      - name: Upload Report
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: ai-agent-report-${{ github.run_id }}
          path: ai-agent-report.md

      - name: Notify Slack
        if: failure()
        uses: 8398a7/action-slack@v3
        with:
          status: ${{ job.status }}
          channel: '#ai-agents'
          webhook_url: ${{ secrets.SLACK_WEBHOOK }}
          message: |
            🚨 AI Agent execution failed
            - Repository: ${{ github.repository }}
            - Event: ${{ github.event_name }}
            - Run: ${{ github.run_id }}

3.2 Issue Auto-Resolution Script

// scripts/ai-agent-issue-resolver.js
const { ClaudeCode } = require('@anthropic/claude-code');
const { Octokit } = require('@octokit/rest');

class IssueResolverAgent {
  constructor() {
    this.claude = new ClaudeCode({
      apiKey: process.env.ANTHROPIC_API_KEY,
      model: 'claude-opus-4'
    });

    this.octokit = new Octokit({
      auth: process.env.GITHUB_TOKEN
    });
  }

  async resolveIssue(issueNumber) {
    console.log(`🤖 Starting AI resolution for issue #${issueNumber}`);

    try {
      // Get issue information
      const { data: issue } = await this.octokit.rest.issues.get({
        owner: process.env.GITHUB_REPOSITORY.split('/')[0],
        repo: process.env.GITHUB_REPOSITORY.split('/')[1],
        issue_number: issueNumber
      });

      // Generate solution with Claude Code
      const solution = await this.claude.solve({
        prompt: `Analyze and solve this GitHub issue:

        Title: ${issue.title}
        Description: ${issue.body}
        Labels: ${issue.labels.map(l => l.name).join(', ')}

        Generate:
        1. Implementation plan
        2. Code changes needed
        3. Test cases
        4. Documentation updates`,

        context: {
          repository: process.env.GITHUB_REPOSITORY,
          branch: 'main',
          files: await this.getRelevantFiles(issue)
        },

        tools: ['file-editor', 'git', 'npm', 'web-search'],
        maxSteps: 100
      });

      // Post results as comment
      await this.octokit.rest.issues.createComment({
        owner: process.env.GITHUB_REPOSITORY.split('/')[0],
        repo: process.env.GITHUB_REPOSITORY.split('/')[1],
        issue_number: issueNumber,
        body: this.formatSolutionComment(solution)
      });

      // Close issue on success
      if (solution.success) {
        await this.octokit.rest.issues.update({
          owner: process.env.GITHUB_REPOSITORY.split('/')[0],
          repo: process.env.GITHUB_REPOSITORY.split('/')[1],
          issue_number: issueNumber,
          state: 'closed',
          labels: [...issue.labels.map(l => l.name), 'ai-resolved']
        });
      }

      console.log(`✅ Issue #${issueNumber} resolved successfully`);
      return { success: true, solution };

    } catch (error) {
      console.error(`❌ Failed to resolve issue #${issueNumber}:`, error);

      // Post comment on error
      await this.octokit.rest.issues.createComment({
        owner: process.env.GITHUB_REPOSITORY.split('/')[0],
        repo: process.env.GITHUB_REPOSITORY.split('/')[1],
        issue_number: issueNumber,
        body: `🚨 AI Agent encountered an error while trying to resolve this issue:

        \`\`\`
        ${error.message}
        \`\`\`

        Please review manually or reassign to a human developer.`
      });

      return { success: false, error: error.message };
    }
  }

  formatSolutionComment(solution) {
    return `## 🤖 AI Agent Solution

### Analysis Summary
${solution.analysis}

### Implementation Plan
${solution.plan.map((step, i) => `${i + 1}. ${step}`).join('\n')}

### Code Changes
${solution.changes.map(change => `
#### ${change.file}
\`\`\`${change.language}
${change.code}
\`\`\`
`).join('\n')}

### Testing Strategy
${solution.tests}

### Estimated Effort
- **Complexity**: ${solution.complexity}/10
- **Time Required**: ${solution.timeEstimate}
- **Risk Level**: ${solution.riskLevel}

---
*This solution was generated by Claude Code Agent. Please review before implementation.*`;
  }

  async getRelevantFiles(issue) {
    // Infer relevant files from issue content
    const keywords = this.extractKeywords(issue.title + ' ' + issue.body);
    const files = [];

    for (const keyword of keywords) {
      const searchResults = await this.claude.searchCodebase({
        query: keyword,
        fileTypes: ['.ts', '.tsx', '.js', '.jsx', '.py', '.java'],
        maxResults: 5
      });
      files.push(...searchResults);
    }

    return files.slice(0, 10); // Maximum 10 files
  }

  extractKeywords(text) {
    // Extract technical keywords
    const techKeywords = text.match(/\b(API|component|function|class|method|endpoint|database|auth|login|user|admin)\b/gi) || [];
    return [...new Set(techKeywords)];
  }
}

// Script execution
if (require.main === module) {
  const issueNumber = process.argv[2];
  if (!issueNumber) {
    console.error('Usage: node ai-agent-issue-resolver.js <issue_number>');
    process.exit(1);
  }

  const agent = new IssueResolverAgent();
  agent.resolveIssue(issueNumber)
    .then(result => {
      process.exit(result.success ? 0 : 1);
    })
    .catch(error => {
      console.error('Fatal error:', error);
      process.exit(1);
    });
}

4. Performance Monitoring and Metrics

4.1 AI Agent Monitoring Dashboard

// monitoring/ai-agent-metrics.ts
export interface AgentMetrics {
  executionTime: number;
  tokensUsed: number;
  apiCalls: number;
  successRate: number;
  errorRate: number;
  cacheHitRate: number;
  costEstimate: number;
}

export class AgentMonitoringService {
  private metrics: Map<string, AgentMetrics[]> = new Map();

  async trackExecution(
    agentId: string, 
    task: string, 
    execution: () => Promise<any>
  ): Promise<any> {
    const startTime = Date.now();
    const startTokens = await this.getCurrentTokenUsage();

    try {
      const result = await execution();

      const endTime = Date.now();
      const endTokens = await this.getCurrentTokenUsage();

      const metrics: AgentMetrics = {
        executionTime: endTime - startTime,
        tokensUsed: endTokens - startTokens,
        apiCalls: this.getAPICallCount(),
        successRate: 1.0,
        errorRate: 0.0,
        cacheHitRate: this.getCacheHitRate(),
        costEstimate: this.calculateCost(endTokens - startTokens)
      };

      this.recordMetrics(agentId, metrics);
      await this.sendMetricsToDatadog(agentId, metrics);

      return result;
    } catch (error) {
      const metrics: AgentMetrics = {
        executionTime: Date.now() - startTime,
        tokensUsed: 0,
        apiCalls: this.getAPICallCount(),
        successRate: 0.0,
        errorRate: 1.0,
        cacheHitRate: 0.0,
        costEstimate: 0
      };

      this.recordMetrics(agentId, metrics);
      await this.sendErrorToSentry(agentId, error);

      throw error;
    }
  }

  generateDailyReport(): string {
    const report = {
      totalExecutions: this.getTotalExecutions(),
      averageExecutionTime: this.getAverageExecutionTime(),
      totalTokensUsed: this.getTotalTokensUsed(),
      totalCost: this.getTotalCost(),
      topFailingTasks: this.getTopFailingTasks(),
      performanceOptimizations: this.suggestOptimizations()
    };

    return `## AI Agent Daily Report

### Execution Summary
- **Total Executions**: ${report.totalExecutions}
- **Average Execution Time**: ${report.averageExecutionTime}ms
- **Total Tokens Used**: ${report.totalTokensUsed.toLocaleString()}
- **Estimated Cost**: $${report.totalCost.toFixed(2)}

### Performance Insights
${report.performanceOptimizations}

### Top Failing Tasks
${report.topFailingTasks.map(task => `- ${task.name}: ${task.failureRate}%`).join('\n')}`;
  }
}

Summary

By implementing Claude Code Hooks configuration and GitHub Copilot Agent Mode, you can achieve:

Implementation Results

  • 7-hour continuous autonomous execution: Full automation from issue resolution to PR creation
  • Automatic error recovery: 99% of common errors are auto-fixed
  • Cost efficiency: Same quality at ⅓ the cost of manual development
  • 24/7 operation: Continuous AI development with GitHub Actions

Implementation Key Points

  • Introduce Hooks configuration incrementally (start with simple ones)
  • Security settings are mandatory (secrets, permissions)
  • Optimize performance through metrics monitoring
  • Error handling and automatic recovery functionality