Skip to content
BotServBotServ
Programming AgentAI AgentCode GenerationDebuggingAutomation

Programming Agent: Write and Debug Code with AI

AI agents for coding. Generate, debug, refactor code and practical examples.

S

schutzgeist

5 min read
Programming Agent: Write and Debug Code with AI

Coding Agent: Writing and Debugging Code with AI

What this article covers

  • What a coding agent is and how it works.
  • How the agent generates, debugs, and refactors code.
  • How to equip the agent with tools for file systems, Git, and testing.
  • Real-world examples for feature development, bug fixes, and code review.
  • Best practices for security, code quality, and human oversight.

Introduction: Understanding the coding agent

A coding agent is an AI agent that writes code autonomously. It understands requirements, plans implementation, writes code, tests it, and fixes errors. Not just “generate a snippet,” but “build a complete feature, test it, debug it.”

This article is for developers who want to use AI agents for programming. You’ll find foundational concepts in AI Agents and Coding Models.

Why use a coding agent?

Imagine implementing a feature: “User authentication with JWT.” A traditional copilot generates code snippets. An agent works differently: analyze requirements, plan architecture, write code, write tests, run tests, fix errors, commit. The agent develops the entire feature end-to-end.

How coding agents work

Requirement → Agent plans (using an LLM) → Call tools (read files, write code, run tests) → Iterate → Feature complete. Git handles versioning, tests ensure quality.

The core idea is simple: deliver complete features, not isolated snippets.

Who should read this?

  • Developers using coding agents.
  • Teams automating feature development.
  • DevOps engineers extending CI/CD with agents.
  • Solo developers looking to boost productivity.

Key concepts

  • AI Agent - An autonomous actor. When useful: learn the concept.
  • Tool-Calling - Invoking tools. When useful: file and Git operations.
  • Coding Models - Specialized for code. When useful: choosing the right model.
  • Ollama - Local model server. When useful: your backend.
  • Code Assistants - Aider, Continue. When useful: tools for the agent.

Architecture

Requirement / Issue
    │
    ▼
Coding Agent (Ollama + Tools)
    │
    ├─ Understand: What needs to be built?
    ├─ Plan: Architecture, files, tests
    ├─ Act: Call tools
    │   ├─ read_file: Read code
    │   ├─ write_file: Write code
    │   ├─ run_tests: Execute tests
    │   ├─ git_commit: Commit changes
    │   └─ run_linter: Check code style
    ├─ Verify: Tests passing? Code clean?
    └─ Iterate: On errors, return to planning
    │
    ▼
Result
    ├─ Code implemented
    ├─ Tests written
    ├─ Git commit created
    └─ Pull request (optional)

Practical example 1: Feature development

class FeatureAgent:
    """Agent for feature development"""

    async def implement(self, requirement):
        """Implement a feature"""
        # 1. Understand requirement
        plan = await self.plan(requirement)

        # 2. Write code
        for file in plan["files"]:
            code = await self.generate_code(file, plan)
            await self.write_file(file["path"], code)

        # 3. Write tests
        tests = await self.generate_tests(plan)
        await self.write_file("test_feature.py", tests)

        # 4. Run tests
        result = await self.run_tests()

        # 5. Debug on errors
        if not result["passed"]:
            fixes = await self.debug(result["errors"])
            await self.apply_fixes(fixes)
            await self.run_tests()

        # 6. Commit
        await self.git_commit(f"feat: {requirement}")

        return plan

Practical example 2: Bug fixing

class BugFixAgent:
    """Agent for bug fixing"""

    async def fix(self, bug_report):
        """Fix a bug"""
        # 1. Reproduce the bug
        error = await self.reproduce(bug_report)

        # 2. Analyze the root cause
        analysis = await self.analyze(error)

        # 3. Generate a fix
        fix = await self.generate_fix(analysis)

        # 4. Apply the fix
        await self.write_file(analysis["file"], fix)

        # 5. Test
        result = await self.run_tests()

        if result["passed"]:
            await self.git_commit(f"fix: {bug_report['title']}")
        else:
            await self.iterate_fix(error)

        return analysis

Practical example 3: Code review

class ReviewAgent:
    """Agent for code review"""

    async def review(self, diff):
        """Review code"""
        analysis = await ollama.generate(f"""
Review this code:
{diff}

Evaluate:
1. Code quality
2. Potential bugs
3. Security risks
4. Performance
5. Best practices

Respond as JSON:
{{"issues": [...], "suggestions": [...], "approved": true|false}}""", format="json")

        return json.loads(analysis)

Tools for coding agents

tools = [
    {
        "name": "read_file",
        "description": "Read a file",
        "function": read_file
    },
    {
        "name": "write_file",
        "description": "Write to a file",
        "function": write_file
    },
    {
        "name": "list_files",
        "description": "List files in directory",
        "function": list_files
    },
    {
        "name": "run_tests",
        "description": "Execute tests",
        "function": run_tests
    },
    {
        "name": "run_linter",
        "description": "Run code linter",
        "function": run_linter
    },
    {
        "name": "git_commit",
        "description": "Commit changes",
        "function": git_commit
    },
    {
        "name": "search_code",
        "description": "Search code",
        "function": search_code
    }
]

Security considerations

  • Code execution: The agent should not execute arbitrary code. See Sandboxing.
  • Git permissions: The agent should not force-push or delete branches. See Tool Permissions.
  • Secrets: The agent must not write secrets into code. See Data Protection.
  • Code quality: Generated code requires review. Human oversight is essential.
  • Prompt injection: Code comments can contain injections. See Prompt Injection.

Common pitfalls

  • Tasks too complex: Give the agent small, clear tasks. Break large features into parts.
  • No tests: Without tests, the agent cannot verify the code works.
  • Vague prompts: “Implement auth” is too broad. Provide precise requirements.
  • No iteration limit: Agents can get stuck in loops. Set max iterations.
  • No fallback: If the agent fails, human development should continue uninterrupted.

Further reading

Key takeaways:

  • Coding agent: understands, plans, writes, tests, commits, all autonomously.
  • Tools: read_file, write_file, run_tests, git_commit, run_linter.
  • Use for feature development, bug fixes, code review.
  • Critical operations need human approval.
  • Run locally with Ollama: your code stays private.

FAQ

What is a coding agent?

An AI agent that writes code autonomously. It understands requirements, plans implementation, writes code, tests it, and fixes errors. It delivers complete features, not just snippets.

What can the agent do?

Implement features, fix bugs, write tests, review code, refactor, document, and commit. It can automate the entire development workflow.

How good is the generated code?

Very good for standard patterns and clear requirements. For complex architecture or unusual requirements, human review is recommended.

Is the agent secure?

Yes, if properly configured. Restrict arbitrary code execution, limit Git permissions, prevent secrets in code, and require human review for critical changes.

Which model for coding?

deepseek-coder-v2, qwen2.5-coder, or codellama. For best results, use deepseek-coder-v2:16b or qwen2.5-coder:14b. See Coding Models.

Does my code stay private?

Yes, when using Ollama locally. Your code never reaches cloud APIs. All analysis and generation happens on your machine.

What does it cost?

Nothing. Ollama and agent frameworks are open source. Only hardware costs for your server. No per-developer licensing fees.

Agent or GitHub Copilot?

Use Copilot for inline snippets while coding. Use agents for complete features, debugging, and automation. You can combine both.

Sources and further reading

Back to Blog
Share:

Related Posts