Examples

Real-world examples to help you get started with CoderClaw. Each example is a complete, working project.

1. Code Analyzer Agent

A simple agent that analyzes code quality, identifies bugs, and suggests improvements.

import { Agent } from 'coderclaw';

const codeAnalyzer = new Agent({
  name: 'CodeAnalyzer',
  model: 'claude-opus',
  instructions: `You are an expert code reviewer. Analyze code for:
    1. Performance issues
    2. Security vulnerabilities
    3. Best practice violations
    4. Code style improvements
    Provide specific, actionable feedback.`,
  tools: [
    {
      name: 'analyze_syntax',
      description: 'Check code syntax and structure'
    },
    {
      name: 'check_security',
      description: 'Scan for security vulnerabilities'
    }
  ]
});

const result = await codeAnalyzer.run({
  task: 'Review this code for issues',
  input: `
    function getUserData(id) {
      const sql = "SELECT * FROM users WHERE id = " + id;
      const result = database.query(sql);
      return result;
    }
  `
});

console.log(result.output);

Key Features: Security analysis, performance optimization, code style guidance

2. Documentation Generator

Automatically generate comprehensive documentation from code with examples and API reference.

import { Agent } from 'coderclaw';

const docGenerator = new Agent({
  name: 'DocGenerator',
  model: 'claude-opus',
  instructions: `Generate clear, comprehensive documentation that includes:
    - Function descriptions
    - Parameter explanations
    - Return value documentation
    - Usage examples
    - Edge cases
  Format output as Markdown.`
});

const result = await docGenerator.run({
  task: 'Generate documentation for this module',
  input: sourceCode
});

// Output is formatted Markdown ready to use
console.log(result.output);

Key Features: Auto-documentation, example generation, type documentation

3. Parallel Testing with Sub-Agents

Use sub-agents to run tests in parallel across different components.

import { Agent, SubAgent } from 'coderclaw';

const parentAgent = new Agent({
  name: 'TestOrchestrator',
  model: 'claude-opus',
  instructions: 'Coordinate testing across multiple components'
});

const testSubAgent = new SubAgent({
  name: 'TestRunner',
  model: 'claude-opus'
});

// Spawn 4 sub-agents to test different modules in parallel
const testAgents = await testSubAgent.spawn(4);

const components = [
  { name: 'auth', path: './src/auth.ts' },
  { name: 'api', path: './src/api.ts' },
  { name: 'db', path: './src/db.ts' },
  { name: 'utils', path: './src/utils.ts' }
];

// Dispatch tasks to sub-agents
const testResults = await Promise.all(
  components.map((comp, i) => 
    testAgents[i].run({
      task: `Test the ${comp.name} module`,
      input: readFileSync(comp.path, 'utf8')
    })
  )
);

await testSubAgent.waitAll();
console.log('All tests completed', testResults);

Key Features: Parallel processing, task distribution, result aggregation

4. Project Refactoring Agent

A sophisticated agent that refactors large codebases while maintaining functionality.

import { Agent } from 'coderclaw';

const refactoringAgent = new Agent({
  name: 'RefactoringExpert',
  model: 'claude-opus',
  instructions: `You are a code refactoring specialist. When given code:
    1. Identify refactoring opportunities
    2. Apply modern patterns and practices
    3. Maintain 100% backward compatibility
    4. Improve readability and maintainability
    5. Provide detailed explanation of changes
  `
});

const result = await refactoringAgent.run({
  task: 'Refactor this legacy code',
  input: legacyCode,
  timeout: 60000
});

console.log('Refactored code:', result.output);
console.log('Changes explained:', result.explanation);

Key Features: Code transformation, pattern matching, compatibility preservation

5. Multi-Agent Mesh Network

Set up a complete mesh network of specialized agents working together through CoderClawLink.

import { Agent } from 'coderclaw';

// Create specialized agents
const codeReviewer = new Agent({
  name: 'CodeReviewer',
  model: 'claude-opus'
});

const testWriter = new Agent({
  name: 'TestWriter',
  model: 'claude-opus'
});

const documentationWriter = new Agent({
  name: 'DocWriter',
  model: 'claude-opus'
});

// Coordinate through CoderClawLink mesh
const meshUrl = 'https://app.coderclaw.ai/mesh';

// Register agents on the mesh
await codeReviewer.register(meshUrl);
await testWriter.register(meshUrl);
await documentationWriter.register(meshUrl);

// Agents can now discover and communicate with each other
// Submit a feature implementation task
const result = await codeReviewer.run({
  task: 'Implement a new feature and coordinate reviews',
  mesh: meshUrl
});

Key Features: Agent mesh networking, service discovery, inter-agent communication

Next Steps

  • Run the examples locally with the provided Docker setup
  • Modify examples for your specific use case
  • Check the API Reference for detailed documentation
  • Share your examples in our showcase