---
title: "Build a Documentation Generator Agent"
description: "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."
slug: "agent-sdk/tutorials/500-documentation-generator"
sidebar:
  label: "Documentation generator"
seo:
  title: "Documentation Generator Agent | Autohand Code"
  description: "Build a documentation generator coding assistant with the Autohand Code Agent SDK. Learn how to create agents that automatically generate documentation from code."
---

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.

:::note
**Prerequisites:**

- Completed [Code Reviewer Agent](/agent-sdk/tutorials/100-code-reviewer-agent)
- Node.js 16+ (for TypeScript), Python 3.10+ (for Python), Java 11+ (for Java), Go 1.16+ (for Go), Swift 5.5+ (for Swift), or Rust 1.60+ (for Rust)
- SDK installed for your chosen language
- API key from your LLM provider (set as `AUTOHAND_API_KEY`)
:::

## 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:

```ts
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:

```ts title="TypeScript"
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"
  )
);
```

```python title="Python"
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"
))
```

```java title="Java"
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"
));
```

```go title="Go"
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
```

```swift title="Swift"
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"
)
```

```rust title="Rust"
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:

```ts title="TypeScript"
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);
```

```python title="Python"
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)
```

```java title="Java"
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);
```

```go title="Go"
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)
```

```swift title="Swift"
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)
```

```rust title="Rust"
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:

```ts
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

**[📁 View the Example](https://github.com/autohandai/agent-sdk-typescript/tree/main/examples)**

See the complete example in the SDK repositories.

**[🧪 Testing Agent](/agent-sdk/tutorials/600-testing-agent)**

Learn how to build agents that generate automated tests.

**[📚 Explore More](/agent-sdk/overview)**

Check out all examples in the SDK repositories.
