How to beginner · 3 min read

How to use AI to write user stories

Quick answer
Use a large language model like gpt-4o to generate user stories by providing clear prompts describing the feature and acceptance criteria. Call the chat.completions.create endpoint with structured messages to get well-formed user stories automatically.

PREREQUISITES

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

Setup

Install the OpenAI Python SDK and set your API key as an environment variable to authenticate requests.

bash
pip install openai>=1.0

Step by step

Use the gpt-4o model to generate user stories by sending a prompt that describes the feature and desired acceptance criteria. The model will return formatted user stories you can integrate into your workflow.

python
import os
from openai import OpenAI

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

prompt = (
    "Write three user stories for a feature that allows users to reset their password via email. "
    "Include acceptance criteria for each story."
)

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

user_stories = response.choices[0].message.content
print(user_stories)
output
As a user, I want to receive a password reset email so that I can regain access to my account.

Acceptance Criteria:
- User can request a password reset link via email.
- Email contains a secure, time-limited reset link.

As a user, I want to be able to set a new password after clicking the reset link so that I can secure my account.

Acceptance Criteria:
- Reset link directs to a password reset form.
- New password must meet security requirements.

As a user, I want to receive confirmation after successfully resetting my password so that I know the process is complete.

Acceptance Criteria:
- Confirmation message is displayed after password reset.
- User can log in with the new password immediately.

Common variations

You can customize the prompt to generate user stories for different features or formats. Use other models like claude-3-5-sonnet-20241022 for more creative outputs or enable streaming for real-time generation.

python
import os
import anthropic

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

prompt = (
    "Generate two user stories for a mobile app feature that allows photo uploads, "
    "including acceptance criteria."
)

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

print(message.content[0].text)
output
As a user, I want to upload photos from my device so that I can share moments with friends.

Acceptance Criteria:
- User can select photos from gallery or camera.
- Photos are uploaded with progress indication.

As a user, I want to edit photos before uploading so that I can enhance them.

Acceptance Criteria:
- Basic editing tools like crop and filters are available.
- Edited photos are saved and uploaded correctly.

Troubleshooting

If the API returns errors or incomplete user stories, check your API key and usage limits. Ensure your prompt is clear and concise to get better results. For rate limits, implement retries with exponential backoff.

Key Takeaways

  • Use clear, detailed prompts to guide AI in generating precise user stories.
  • Leverage gpt-4o or claude-3-5-sonnet-20241022 for high-quality story generation.
  • Always handle API errors and rate limits gracefully in production code.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗