Code Generation with Claude API — Python

How to generate, explain, refactor, and review code using the Claude API in Python. Covers function generation, docstring writing, unit test generation, and code review with structured feedback.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Claude is purpose-built for code generation tasks. It understands context across thousands of lines, generates idiomatic code for 30+ languages, and can explain, refactor, and review code with structured feedback. This guide covers every common code generation pattern.

Installation

pip install anthropic

Generate a function from a description

import anthropic

client = anthropic.Anthropic()

def generate_function(description: str, language: str = "Python") -> str:
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=2048,
        system=f"You are an expert {language} developer. Return only the code — no explanation, no markdown fences.",
        messages=[{"role": "user", "content": description}]
    )
    return message.content[0].text

code = generate_function(
    "A function that takes a list of dicts, each with 'name' and 'score' keys, "
    "and returns the top N by score. Handle ties by keeping all tied entries."
)
print(code)

Generate with structured JSON output (reliable parsing)

import json

def generate_with_explanation(description: str) -> dict:
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=4096,
        system=(
            "Return a JSON object with keys: "
            "'code' (the complete function as a string), "
            "'language' (detected target language), "
            "'dependencies' (list of required packages). "
            "No markdown fences in the JSON values."
        ),
        messages=[{"role": "user", "content": description}]
    )
    return json.loads(message.content[0].text)

result = generate_with_explanation(
    "A Python async function that fetches JSON from a URL with a 10s timeout and retries up to 3 times on failure"
)
print(result["code"])
print("Dependencies:", result["dependencies"])  # ["aiohttp", "asyncio"]

Generate docstrings for existing code

def add_docstrings(source_code: str) -> str:
    message = client.messages.create(
        model="claude-haiku-4-5-20251001",  # fast + cheap for docstrings
        max_tokens=4096,
        system=(
            "Add Google-style docstrings to every function and class in the provided code. "
            "Do not change any logic or variable names. Return only the updated code, no explanation."
        ),
        messages=[{"role": "user", "content": source_code}]
    )
    return message.content[0].text

Generate unit tests with pytest

def generate_tests(source_code: str) -> str:
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=4096,
        system=(
            "Write pytest unit tests for the provided Python code. "
            "Include: normal cases, edge cases, and expected exceptions. "
            "Use fixtures where appropriate. Return only the test code."
        ),
        messages=[
            {"role": "user", "content": f"Write tests for:

{source_code}"}
        ]
    )
    return message.content[0].text

Code review with structured feedback

def review_code(source_code: str, language: str = "Python") -> dict:
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=2048,
        system=(
            f"You are a senior {language} code reviewer. "
            "Return a JSON object with keys: "
            "'issues' (list of {severity: 'critical'|'warning'|'info', line: int|null, message: str}), "
            "'summary' (one sentence), "
            "'approved' (boolean: true if no critical issues). "
            "No markdown fences."
        ),
        messages=[{"role": "user", "content": f"Review this code:

{source_code}"}]
    )
    return json.loads(message.content[0].text)

review = review_code("""
def divide(a, b):
    return a / b
""")
# {"issues": [{"severity": "critical", "line": 2, "message": "No zero-division guard"}], "approved": false}

Refactor existing code

def refactor(source_code: str, instruction: str) -> str:
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=4096,
        system="Refactor the provided code per the instruction. Preserve all behavior. Return only the refactored code.",
        messages=[
            {"role": "user", "content": f"Instruction: {instruction}

Code:
{source_code}"}
        ]
    )
    return message.content[0].text

# Examples:
# refactor(code, "Convert to async/await")
# refactor(code, "Extract repeated logic into a helper function")
# refactor(code, "Replace for loop with list comprehension where possible")

Model comparison for code generation

TaskRecommended modelWhy
Complex function generationclaude-sonnet-4-6Better reasoning for multi-step logic
Docstring generationclaude-haiku-4-5-20251001Simple pattern, 10× cheaper
Unit test generationclaude-sonnet-4-6Edge case discovery requires reasoning
Code reviewclaude-sonnet-4-6Critical security/logic issues need depth
Simple autocompleteclaude-haiku-4-5-20251001Speed + cost for editor integration
Architecture designclaude-opus-4-7Long-context multi-file reasoning

Estimate your API costs before building an editor integration with the Claude API Cost Calculator. For structured data extraction from code (e.g., parse function signatures into JSON), see the data extraction guide.

Frequently asked questions

Which Claude model is best for code generation?
Use `claude-sonnet-4-6` for complex code generation tasks (multi-function modules, architectural decisions, bug-fix reasoning). Use `claude-haiku-4-5-20251001` for simple completions, docstrings, and unit test stubs — it's 10× cheaper and fast enough for editor integrations.
How do I get Claude to return only code without explanation?
Set a system prompt: 'Return only the code. No explanation, no markdown fences, no preamble.' For structured output, ask for JSON with a `code` key and parse it — this is more robust than stripping markdown fences.
Can Claude generate code in any language?
Yes. Claude generates idiomatic code in 30+ languages including Python, TypeScript, Go, Rust, Java, C++, Ruby, and SQL. Specify the language explicitly in the prompt and include the relevant framework or library name for best results.
How do I generate unit tests for existing code?
Pass the source function in the user message with the instruction 'Write pytest unit tests for the following function. Cover edge cases and include docstrings.' Claude generates test cases including boundary conditions and exception paths.
Is Claude-generated code safe to use in production?
Always review generated code before deploying. Claude occasionally generates plausible-looking but incorrect logic, especially for security-sensitive code (authentication, cryptography, SQL queries). Use the code review pattern in this guide to have Claude self-audit before you ship.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript