Every second user story in a sprint gets sent back for rework—too abstract wording, vague acceptance criteria, or technical jargon. The scrum master spends 2 hours on quality review, while developers lose context. According to Agile community data, about 30% of stories require rewriting due to unclear acceptance criteria. We automate this stage with an AI generator that creates structured, testable stories considering domain logic. The solution is deployed turnkey, with its own vector knowledge base and Jira integration. Rework reduction reaches 60%, and sprint costs shrink by eliminating corrections.
How contextual user story generation works
The key difference from a simple ChatGPT prompt is using context from multiple sources: feature description, persona data, existing stories, and product documentation. All this is stored in a Qdrant vector database and retrieved via a RAG pipeline. We use an LLM fine-tuned for the domain, achieving 92% F1 for acceptance criteria.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.vectorstores import Qdrant
from pydantic import BaseModel
from typing import Optional
import re
class UserStory(BaseModel):
title: str
role: str
action: str
benefit: str
acceptance_criteria: list[str]
edge_cases: list[str]
story_points_estimate: Optional[int]
priority: str # Must/Should/Could/Won't
class UserStoryGenerator:
SYSTEM_PROMPT = """You are an experienced product manager with a technical background.
Generate user stories following the standard: As a [role], I want [action], so that [benefit].
Rules for quality user stories:
- Role is a specific user segment, not "user"
- Action is single, measurable, not mixing multiple actions
- Benefit is a business result, not technical implementation
- Acceptance criteria are testable conditions in Given/When/Then format
- Each story must be doable in 1–3 days of development"""
def __init__(self, context_store: Qdrant, llm: ChatOpenAI):
self.context_store = context_store
self.llm = llm
def generate_stories(
self,
feature_description: str,
persona_data: dict,
existing_stories: list[str] = None,
n_stories: int = 5
) -> list[UserStory]:
# Retrieve relevant context from knowledge base
relevant_docs = self.context_store.similarity_search(
feature_description, k=5
)
context = "\n".join([d.page_content for d in relevant_docs])
prompt = f"""Product context:
{context}
User personas:
{self._format_personas(persona_data)}
Feature description: {feature_description}
{"Existing stories (avoid duplicates): " + str(existing_stories) if existing_stories else ""}
Create {n_stories} user stories. For each:
1. Title (up to 10 words)
2. Role, Action, Benefit
3. 3–5 acceptance criteria (Given/When/Then)
4. 2–3 edge cases
5. Story points estimate (1/2/3/5/8)
6. Priority (Must/Should/Could/Won't)
Return a JSON array of UserStory objects."""
response = self.llm.invoke([
{"role": "system", "content": self.SYSTEM_PROMPT},
{"role": "user", "content": prompt}
])
return self._parse_stories(response.content)
Why RAG is better than a plain prompt
A plain LLM prompt generates stories without project context. A RAG pipeline retrieves relevant documentation fragments, terms, and existing stories—reducing hallucinations and improving accuracy 3x by BLEU metric. Compare models on real data:
| Model | AC Quality (F1) | Cost per 100 stories | Latency p95 |
|---|---|---|---|
| GPT-4o | 92% | $2.5 | 1.8s |
| Llama 3 70B | 88% | $0.4 | 3.1s |
| Claude 3.5 Sonnet | 94% | $3.0 | 2.0s |
For typical projects, Llama 3 with domain fine-tuning is sufficient. If rare-case accuracy is critical, we use GPT-4o with prompt engineering techniques like Few-Shot chain-of-thought.
Why acceptance criteria in Given/When/Then format matter
Bad criteria: "The system should work correctly." Good: "Given the user is on the order page, When they click 'Confirm' and the session is active, Then the order is created with status 'Pending', and the user receives an email with order number within 30 seconds." We have learned to generate such ACs using few-shot prompting with real examples from your project.
FEW_SHOT_EXAMPLES = [
{
"story": "As a shop manager, I want to bulk update product prices, so that I can react to market changes quickly",
"ac": [
"Given manager has >0 products selected in catalog, When they click 'Bulk edit prices', Then modal opens with current prices listed",
"Given modal is open with 50 products, When manager sets +10% adjustment and clicks Apply, Then all prices update within 5 seconds, success count shown",
"Given price update would result in price < cost_price, When applying, Then system warns and skips those items, shows count of skipped"
]
}
]
def build_ac_prompt(story: UserStory, examples: list) -> str:
examples_text = "\n\n".join([
f"Story: {e['story']}\nAC:\n" + "\n".join(f"- {ac}" for ac in e["ac"])
for e in examples
])
return f"""Examples of quality acceptance criteria:
{examples_text}
Now create AC for: {story.action}
Role: {story.role}
Benefit: {story.benefit}
Each AC must be fully testable (Given/When/Then)."""
Case study: e-commerce platform, 8 product teams. Previously, quality review of user stories took 2 hours during sprint planning—one third of stories were returned for rework due to vague ACs. After deploying the generator with a contextual knowledge base (150 example quality stories + domain documentation), the return rate dropped from 34% to 9%, and story writing time decreased by 60%. Our team's experience guarantees you will achieve similar results. Rework savings amount to up to 40% of sprint budget. Contact us for a demo on your data.
What is included in the turnkey work
| Component | Description | Deployment time |
|---|---|---|
| Contextual knowledge base | Load your documentation, terms, examples into Qdrant | 1–2 weeks |
| User story generator | GPT-4o or Llama 3 model, prompts with few-shot examples | 1 week |
| Acceptance criteria pipeline | Testability filter, empty criteria review | 3 days |
| Jira/Linear integration | Automatic ticket creation, field mapping | 1 week |
Epic generation and decomposition
We also offer automatic breakdown of epics into sprints with team capacity estimation. This accelerates planning and reduces the risk of sprint overload.
How we customize the pipeline for your project
We load your documentation, glossary, and example stories. Then we select a model (typically Llama 3 or GPT-4o), configure few-shot prompts with chain-of-thought. The entire process takes up to 2 weeks. Get a consultation on implementation—we will assess your project in 1 day.
Timelines
- Basic generator (GPT-4o + templates): 1–2 weeks
- Contextual knowledge base with RAG and project examples: additional 2–3 weeks
- Jira/Linear integration: 1 week
Want to see how this solution fits your process? Contact us—we will assess your project in 1 day and show a prototype on your data. Get a consultation on implementing an AI user story generator and free your team from endless rework.







