Skip to content
Esc
↑↓navigate↵open⌘Jpreview
Start
On this page

Build a Documentation Generator Agent

Learn how to create a coding assistant that automatically generates documentation from code with the Autohand Code Agent SDK. Available in TypeScript, Python, Java, Go, Swift, and Rust.

In this tutorial, you’ll build a documentation generator coding assistant that reads source code, understands its structure, and automatically generates comprehensive documentation. This demonstrates how to use the Agent SDK for documentation maintenance.

What You’ll Build

A documentation generator coding assistant that:

  • Reads source code files
  • Identifies functions, classes, and methods
  • Generates README.md documentation
  • Creates inline code comments
  • Generates API documentation in Markdown
  • Updates existing documentation

Step 1: Create Sample Code

Create a sample code file to document in your chosen language:

import * as fs from "fs";
import * as path from "path";

function createSampleCode(tmpdir: string): void {
  const code = "export function total(items: number[]) { return items.reduce((sum, n) => sum + n, 0); }\n";
  fs.writeFileSync(path.join(tmpdir, "utils.ts"), code);
}

Step 2: Create Documentation Agent

Create an agent with documentation generation capabilities in your chosen language:

import { Agent, Runner } from "@autohandai/agent-sdk";
import { OpenRouterProvider } from "@autohandai/agent-sdk";

const docGenerator = new Agent(
  "Documentation Generator",
  "You are a documentation expert. Read source code files, understand their structure, and generate comprehensive documentation. Create README.md files, inline comments, and API documentation in Markdown format. Document all functions, classes, and their parameters.",
  ["read_file", "write_file", "search_replace", "find_grep"],
  15
);

docGenerator.setProvider(
  new OpenRouterProvider(
    process.env.AUTOHAND_API_KEY!,
    "z-ai/glm-5.2"
  )
);
from autohand_agent_sdk import Agent, Runner, OpenRouterProvider

doc_generator = Agent(
    name="Documentation Generator",
    instructions="You are a documentation expert. Read source code files, understand their structure, and generate comprehensive documentation. Create README.md files, inline comments, and API documentation in Markdown format. Document all functions, classes, and their parameters.",
    tools=["read_file", "write_file", "search_replace", "find_grep"],
    max_turns=15
)

doc_generator.set_provider(OpenRouterProvider(
    api_key=os.getenv("AUTOHAND_API_KEY"),
    model="z-ai/glm-5.2"
))
import ai.autohand.agent.sdk.*;
import ai.autohand.agent.sdk.providers.*;

Agent docGenerator = new Agent(
    "Documentation Generator",
    "You are a documentation expert. Read source code files, understand their structure, and generate comprehensive documentation. Create README.md files, inline comments, and API documentation in Markdown format. Document all functions, classes, and their parameters.",
    DefaultToolRegistry.getTools("read_file", "write_file", "search_replace", "find_grep"),
    15
);

docGenerator.setProvider(new OpenRouterProvider(
    System.getenv("AUTOHAND_API_KEY"),
    "z-ai/glm-5.2"
));
package main

import (
    "os"
    "github.com/autohandai/agentsdk-go/pkg/autohand"
)

docGenerator := autohand.Agent{
    Name:         "Documentation Generator",
    Instructions: "You are a documentation expert. Read source code files, understand their structure, and generate comprehensive documentation. Create README.md files, inline comments, and API documentation in Markdown format. Document all functions, classes, and their parameters.",
    Tools:        []autohand.Tool{autohand.ReadFile, autohand.WriteFile, autohand.EditFile, autohand.Glob},
    MaxTurns:     15,
}

provider := autohand.NewOpenRouterProvider(
    os.Getenv("AUTOHAND_API_KEY"),
    "z-ai/glm-5.2",
)
docGenerator.Provider = &provider
import AutohandAgentSDK

let docGenerator = Agent(
    name: "Documentation Generator",
    instructions: "You are a documentation expert. Read source code files, understand their structure, and generate comprehensive documentation. Create README.md files, inline comments, and API documentation in Markdown format. Document all functions, classes, and their parameters.",
    tools: [.readFile, .writeFile, .editFile, .glob],
    maxTurns: 15
)

docGenerator.provider = try OpenRouterProvider(
    apiKey: ProcessInfo.processInfo.environment["AUTOHAND_API_KEY"] ?? "your-api-key",
    model: "z-ai/glm-5.2"
)
use autohand_agents::{AgentBuilder, Runner};
use autohand_agents::providers::ProviderFactory;
use autohand_agents::config::ConfigLoader;

let mut doc_generator = AgentBuilder::new()
    .name("Documentation Generator")
    .instructions("You are a documentation expert. Read source code files, understand their structure, and generate comprehensive documentation. Create README.md files, inline comments, and API documentation in Markdown format. Document all functions, classes, and their parameters.")
    .tools(vec![Tool::ReadFile, Tool::WriteFile, Tool::EditFile, Tool::Glob])
    .max_turns(15)
    .build()?;

let config = ConfigLoader::from_env()?;
let provider = ProviderFactory::from_env(&config.provider)?;
doc_generator.set_provider(provider);

Step 3: Generate Documentation

Run the documentation generator agent on your sample code in your chosen language:

const result = await Runner.run(
  docGenerator,
  "Read all source files in this directory. Generate a comprehensive README.md file that documents all functions, their parameters, return types, and usage examples. Also create inline comments for any undocumented functions."
);

console.log(result.finalOutput);
result = Runner.run(
    doc_generator,
    "Read all source files in this directory. Generate a comprehensive README.md file that documents all functions, their parameters, return types, and usage examples. Also create inline comments for any undocumented functions."
)

print(result.final_output)
String result = Runner.run(
    docGenerator,
    "Read all source files in this directory. Generate a comprehensive README.md file that documents all functions, their parameters, return types, and usage examples. Also create inline comments for any undocumented functions."
);

System.out.println(result);
result, err := autohand.Runner.Run(&docGenerator, "Read all source files in this directory. Generate a comprehensive README.md file that documents all functions, their parameters, return types, and usage examples. Also create inline comments for any undocumented functions.")
if err != nil {
    log.Fatal(err)
}
fmt.Println(result)
let result = try Runner.run(
    docGenerator,
    "Read all source files in this directory. Generate a comprehensive README.md file that documents all functions, their parameters, return types, and usage examples. Also create inline comments for any undocumented functions."
)

print(result)
let result = Runner::run(&doc_generator, "Read all source files in this directory. Generate a comprehensive README.md file that documents all functions, their parameters, return types, and usage examples. Also create inline comments for any undocumented functions.").await?;
println!("{}", result.final_output);

Key Concepts

Code Understanding

The agent reads and analyzes source code to understand its structure and purpose.

Documentation Generation

Using WRITE_FILE to create comprehensive README.md and documentation files.

Inline Comments

Using EDIT_FILE to add inline documentation comments directly in source code.

API Documentation

Generating API documentation in Markdown format with examples.

Complete Example

Here’s the complete example with cleanup:

import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { Agent, Runner } from "@autohandai/agent-sdk";

async function main() {
  const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), "docs-agent-example-"));
  const oldCwd = process.cwd();
  try {
    fs.writeFileSync(path.join(tmpdir, "utils.ts"), "export function total(items: number[]) { return items.reduce((sum, n) => sum + n, 0); }\n");
    const agent = new Agent("Documentation Generator", "Read source files and create useful Markdown docs.", ["read_file", "write_file", "search_replace", "find_grep"], 15);
    process.chdir(tmpdir);
    const result = await Runner.run(agent, "Document utils.ts and create README.md with an example.");
    console.log(result.finalOutput);
  } finally {
    process.chdir(oldCwd);
    fs.rmSync(tmpdir, { recursive: true, force: true });
  }
}

main();

Real-World Applications

This pattern can be applied to:

  • API documentation: Auto-generate API docs from source code
  • README maintenance: Keep project READMEs up to date automatically
  • Code comments: Add documentation to undocumented code
  • Migration guides: Generate documentation for version upgrades
  • Onboarding: Create developer onboarding documentation

Next Steps

Last updated on

Was this helpful?

Send Feedback

Help us improve this documentation page. Your feedback is valuable.

We'll only contact you if we need clarification