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

# Programming Agent

> Learn how to create AI agents for code development, analysis, and execution.

Write, run, and debug code with a single Agent equipped with execution and analysis tools.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.tools import execute_code, analyze_code

agent = Agent(
    name="Programmer",
    instructions="You are a programming agent. Write and execute code.",
    tools=[execute_code, analyze_code],
)

agent.start("Write a Python script to calculate fibonacci numbers")
```

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "Programming Agent"
        User[📋 Code Request] --> Agent[🤖 Agent]
        Agent --> Tools[🧠 Code Tools]
        Tools --> Result[✅ Working Code]
    end

    classDef input fill:#6366F1,stroke:#7C90A0,color:#fff
    classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef output fill:#10B981,stroke:#7C90A0,color:#fff

    class User,Agent input
    class Tools process
    class Result output
```

Code development agent with execution, analysis, and shell tools.

<Note>
  This page uses a plain `Agent` with code tools — ideal as a task-focused recipe. For the dedicated `CodeAgent` class with sandboxed `generate`/`execute`/`review`/`refactor` methods, see the [Code Agent](/docs/agents/code) page.
</Note>

## Quick Start

<Steps>
  <Step title="Simple Usage">
    Attach code tools and ask for a script.

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent
    from praisonaiagents.tools import execute_code, analyze_code

    agent = Agent(
        name="Programmer",
        instructions="You are a programming agent. Write and execute code.",
        tools=[execute_code, analyze_code],
    )

    agent.start("Write a Python script to calculate fibonacci numbers")
    ```
  </Step>

  <Step title="With Configuration">
    Enable reflection so the agent self-checks its output before returning.

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent
    from praisonaiagents.tools import execute_code, analyze_code

    agent = Agent(
        name="Programmer",
        instructions="Write, execute, and verify code before returning it.",
        tools=[execute_code, analyze_code],
        memory=True,
        reflection=True,
    )

    agent.start("Write and test a quicksort implementation.")
    ```
  </Step>
</Steps>

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Tools as Code Tools

    User->>Agent: "Write a fibonacci script"
    Agent->>Tools: execute_code / analyze_code
    Tools-->>Agent: Output, errors, analysis
    Agent-->>User: Working solution
```

***

## Simple

**Agents: 1** — Single agent with code tools handles writing and executing code.

### Workflow

1. Receive code request
2. Generate code
3. Execute and test
4. Return working solution

### Setup

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install praisonaiagents praisonai
export OPENAI_API_KEY="your-key"
```

### Run — Python

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.tools import execute_code, analyze_code

agent = Agent(
    name="Programmer",
    instructions="You are a programming agent. Write and execute code.",
    tools=[execute_code, analyze_code]
)

result = agent.start("Write a Python script to calculate fibonacci numbers")
print(result)
```

### Run — CLI

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "Write a Python function to sort a list"
```

### Run — agents.yaml

```yaml theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
framework: praisonai
topic: Code Development
roles:
  programmer:
    role: Software Developer
    goal: Write and execute code
    backstory: You are an expert programmer
    tools:
      - execute_code
      - analyze_code
    tasks:
      write_code:
        description: Write a Python script to calculate fibonacci numbers
        expected_output: Working Python code
```

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai agents.yaml
```

### Serve API

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.tools import execute_code, analyze_code

agent = Agent(
    name="Programmer",
    instructions="You are a programming agent.",
    tools=[execute_code, analyze_code]
)

agent.launch(port=8080)
```

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
curl -X POST http://localhost:8080/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Write a function to reverse a string"}'
```

***

## Advanced Workflow (All Features)

**Agents: 1** — Single agent with memory, persistence, structured output, and session resumability.

### Workflow

1. Initialize session for code project tracking
2. Configure SQLite persistence for code history
3. Generate and execute code with structured output
4. Store code in memory for iterative development
5. Resume session for code modifications

### Setup

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install praisonaiagents praisonai pydantic
export OPENAI_API_KEY="your-key"
```

### Run — Python

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent, Task, AgentTeam, Session
from praisonaiagents.tools import execute_code, analyze_code
from pydantic import BaseModel

# Structured output schema
class CodeResult(BaseModel):
    language: str
    code: str
    output: str
    explanation: str

# Create session for project tracking
session = Session(session_id="code-001", user_id="user-1")

# Agent with memory and tools
agent = Agent(
    name="Programmer",
    instructions="Write, execute, and return structured code results.",
    tools=[execute_code, analyze_code],
    memory=True,
    reflection=True
)

# Task with structured output
task = Task(
    description="Write a Python script to calculate fibonacci numbers",
    expected_output="Structured code result",
    agent=agent,
    output_pydantic=CodeResult
)

# Run with SQLite persistence
agents = AgentTeam(
    agents=[agent],
    tasks=[task],
    memory=True
)

result = agents.start()
print(result)

# Resume later
session2 = Session(session_id="code-001", user_id="user-1")
history = session2.search_memory("fibonacci")
```

### Run — CLI

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "Write fibonacci code" --memory --verbose
```

### Run — agents.yaml

```yaml theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
framework: praisonai
topic: Code Development
memory: true
memory_config:
  provider: sqlite
  db_path: code.db
roles:
  programmer:
    role: Software Developer
    goal: Write and execute code with structured output
    backstory: You are an expert programmer
    tools:
      - execute_code
      - analyze_code
    memory: true
    tasks:
      write_code:
        description: Write a Python script to calculate fibonacci numbers
        expected_output: Structured code result
        output_json:
          language: string
          code: string
          output: string
          explanation: string
```

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai agents.yaml --verbose
```

### Serve API

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.tools import execute_code, analyze_code

agent = Agent(
    name="Programmer",
    instructions="Write and execute code.",
    tools=[execute_code, analyze_code],
    memory=True
)

agent.launch(port=8080)
```

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
curl -X POST http://localhost:8080/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Write sorting code", "session_id": "code-001"}'
```

***

## Monitor / Verify

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "test code" --verbose
```

## Cleanup

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
rm -f code.db
```

## Features Demonstrated

| Feature           | Implementation               |
| ----------------- | ---------------------------- |
| Workflow          | Multi-step code development  |
| DB Persistence    | SQLite via `memory_config`   |
| Observability     | `--verbose` flag             |
| Tools             | execute\_code, analyze\_code |
| Resumability      | `Session` with `session_id`  |
| Structured Output | Pydantic `CodeResult` model  |

## Best Practices

<AccordionGroup>
  <Accordion title="Let the agent execute, then verify">
    Attach `execute_code` so the agent runs its own output and catches errors before returning. Code that only looks right often fails on the first run.
  </Accordion>

  <Accordion title="Enable reflection for tricky tasks">
    Set `reflection=True` so the agent reviews and corrects its solution. This trades a few extra tokens for markedly higher first-try success on non-trivial code.
  </Accordion>

  <Accordion title="Prefer the CodeAgent for sandboxed workflows">
    When you need process isolation, timeouts, and language restrictions, use the dedicated `CodeAgent` class instead of a plain Agent with tools.
  </Accordion>

  <Accordion title="Never disable the sandbox on untrusted input">
    If you switch to `CodeAgent`, keep `sandbox=True` in production. Executing model-generated code without isolation is a security risk.
  </Accordion>
</AccordionGroup>

## Related

<CardGroup cols={2}>
  <Card icon="code" href="/docs/agents/code">
    The dedicated CodeAgent class with sandboxed execution.
  </Card>

  <Card icon="chart-line" href="/docs/agents/data-analyst">
    Analyze data files with a data-focused agent.
  </Card>
</CardGroup>
