How to beginner · 3 min read

How to use Claude API for code review

Quick answer
Use the anthropic Python SDK with your ANTHROPIC_API_KEY to call client.messages.create on a claude-3-5-sonnet-20241022 model, sending your code as user input and prompting Claude to review it. Parse the response from response.content[0].text for the review output.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key (free tier available)
  • pip install anthropic>=0.20

Setup

Install the official anthropic Python SDK and set your API key as an environment variable.

  • Install SDK: pip install anthropic
  • Set environment variable: export ANTHROPIC_API_KEY='your_api_key' (Linux/macOS) or set ANTHROPIC_API_KEY=your_api_key (Windows)
bash
pip install anthropic

Step by step

Use the anthropic.Anthropic client to send your code snippet with a prompt asking Claude to perform a code review. The example below shows a synchronous call with a Python function for review.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

code_to_review = '''
def add_numbers(a, b):
    return a + b
'''

prompt = f"""Please review the following Python code for correctness, style, and potential improvements:\n\n{code_to_review}"""

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=512,
    system="You are a helpful assistant specialized in code review.",
    messages=[{"role": "user", "content": prompt}]
)

print("Code review output:\n", response.content[0].text)
output
Code review output:
The function `add_numbers` correctly adds two numbers and returns the result. It is simple and clear. For style, consider adding type hints:

```python
def add_numbers(a: int, b: int) -> int:
    return a + b
```

Also, adding a docstring would improve code documentation.

Common variations

You can customize the code review by:

  • Using different Claude models like claude-sonnet-4-5 for faster or more detailed reviews.
  • Adjusting max_tokens for longer or shorter responses.
  • Implementing async calls with asyncio and client.messages.acreate for non-blocking code review.
python
import asyncio
import os
import anthropic

async def async_code_review():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    code = '''
    def multiply(x, y):
        return x * y
    '''
    prompt = f"Review this code:\n\n{code}"
    response = await client.messages.acreate(
        model="claude-3-5-sonnet-20241022",
        max_tokens=512,
        system="You are a helpful assistant specialized in code review.",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Async code review output:\n", response.content[0].text)

asyncio.run(async_code_review())
output
Async code review output:
The function `multiply` correctly multiplies two numbers. It is concise and clear. Consider adding type hints and a docstring for better clarity.

Troubleshooting

  • If you get authentication errors, verify your ANTHROPIC_API_KEY environment variable is set correctly.
  • If responses are cut off, increase max_tokens.
  • For unexpected output, ensure your prompt clearly asks for a code review and includes the code snippet.

Key Takeaways

  • Use the official anthropic SDK with claude-3-5-sonnet-20241022 for code review tasks.
  • Send your code as user input with a clear prompt requesting a review for best results.
  • Adjust max_tokens and model choice to balance detail and speed.
  • Async calls are supported via client.messages.acreate for scalable workflows.
  • Always set your API key securely in environment variables to avoid authentication issues.
Verified 2026-04 · claude-3-5-sonnet-20241022, claude-sonnet-4-5
Verify ↗