How to beginner · 3 min read

How to use markdown in prompts

Quick answer
Use markdown syntax directly in your prompt text to format lists, code blocks, headings, and emphasis. Most modern AI models like gpt-4o and claude-3-5-sonnet-20241022 interpret markdown to produce clearer, structured responses.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0

Step by step

Send a prompt containing markdown syntax to the gpt-4o model using the OpenAI SDK v1. The model will interpret markdown formatting such as lists and code blocks in its response.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

prompt = """
Here is a list of tasks:

- Write code
- Test code
- Deploy code

And here is a code snippet:

```python
def greet():
    return 'Hello, world!'
```
"""

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
output
Here is a list of tasks:

- Write code
- Test code
- Deploy code

And here is a code snippet:

```python
def greet():
    return 'Hello, world!'
```

Common variations

You can use markdown in prompts with other models like claude-3-5-sonnet-20241022 or stream responses asynchronously. Markdown works similarly across models and SDKs.

python
import os
from anthropic import Anthropic

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

prompt = """
# Tasks

- Review code
- Optimize performance

```javascript
function hello() {
  return 'Hi!';
}
```
"""

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

print(message.content[0].text)
output
# Tasks

- Review code
- Optimize performance

```javascript
function hello() {
  return 'Hi!';
}
```

Troubleshooting

If markdown formatting does not appear as expected, ensure your prompt uses proper markdown syntax and that the model supports markdown rendering. Avoid mixing markdown with incompatible formatting or escaping characters unnecessarily.

Key Takeaways

  • Use standard markdown syntax directly in prompts to improve AI response clarity.
  • Most modern models like gpt-4o and claude-3-5-sonnet-20241022 support markdown interpretation.
  • Include code blocks and lists in prompts to structure instructions or examples clearly.
  • Test markdown rendering by printing the model's raw output to verify formatting.
  • Avoid escaping markdown characters unnecessarily to ensure proper formatting.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗