> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Mode

> AI-powered code generation and editing tool

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    U[Input] --> A[Agent]
    A --> T[Tool]
    T --> O[Output]

    classDef agent fill:#8B0000,color:#fff
    classDef tool fill:#189AB4,color:#fff

    class A agent
    class U,O tool
    class T tool
```

# Code Mode

Code Mode provides AI-powered code generation and editing capabilities for agents.

## Installation

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
npm install praisonai-ts
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    import { Agent, codeMode } from 'praisonai-ts';

    const codeTool = codeMode({
      language: 'typescript',
    });

    const agent = new Agent({
      name: 'CodeAgent',
      instructions: 'You help write and edit code.',
      tools: [codeTool],
    });

    const response = await agent.chat('Write a function to calculate fibonacci numbers');
    ```
  </Step>

  <Step title="With Configuration">
    Adjust agent instructions, tool options, and provider settings for production — see the Configuration section below.
  </Step>
</Steps>

## Configuration

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
interface CodeModeConfig {
  /** Programming language */
  language?: 'typescript' | 'javascript' | 'python' | 'go' | 'rust';
  /** Enable code formatting */
  format?: boolean;
  /** Enable linting */
  lint?: boolean;
  /** Working directory */
  workDir?: string;
}
```

## Usage Examples

### Generate Code

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
const codeTool = codeMode({ language: 'python' });

const agent = new Agent({
  name: 'PythonCoder',
  instructions: 'You write Python code.',
  tools: [codeTool],
});

await agent.chat('Create a class for managing a todo list');
```

### Edit Existing Code

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
const codeTool = codeMode({
  language: 'typescript',
  workDir: './src',
});

const agent = new Agent({
  name: 'CodeEditor',
  instructions: 'You help refactor and improve code.',
  tools: [codeTool],
});

await agent.chat('Refactor the utils.ts file to use async/await');
```

## Input/Output Types

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
interface CodeModeInput {
  /** Action to perform */
  action: 'generate' | 'edit' | 'explain' | 'review';
  /** Code or description */
  content: string;
  /** Target file path (for edit) */
  filePath?: string;
}

interface CodeModeResult {
  /** Generated or edited code */
  code: string;
  /** Explanation */
  explanation?: string;
  /** File path if saved */
  filePath?: string;
  /** Language */
  language: string;
}
```

## Environment Variables

| Variable         | Required | Description         |
| ---------------- | -------- | ------------------- |
| `OPENAI_API_KEY` | Yes      | For code generation |

## Related

<CardGroup cols={2}>
  <Card title="Code Execution" icon="book" href="/docs/js/tools/code-execution">
    Execute code
  </Card>

  <Card title="Custom Tools" icon="book" href="/docs/js/customtools">
    Create custom tools
  </Card>
</CardGroup>
