Most “prompt engineering” advice is vague nonsense. “Be specific.” “Provide context.” Thanks, very helpful.
Here are 7 concrete patterns I use daily that make a measurable difference when working with AI coding tools.
Pattern 1: The Constraint Sandwich# Structure: Task → Constraints → Task reminder
W C - - - - - W r o r i n U M R I M i t s s u e n a t e t e s t c x e r t u l a a o r u 3 t i n h n d 0 h P n l a e e y t y n a l t s d t i f h : t l t y n u o h e u p e n n e p e s c i l t f s n e h i u t t i o n a e o n n c n r f t t d n s w i a a ( i o r t i t n d i s h o _ t l n v t h i a a h a b l l e t r i s a e d e v r m : a y a e l i b x i ( l o a d n o c a o a l t t d , e t d c s h r r o i e e n e r s a s m d s s t a - e o r i p s n a l a : i r ( n a t R s t d y F t s d C r . r p ) e a 6 s c 5 s k 3 e a 1 s g ) . e s )
Why it works: AI models tend to forget constraints mentioned at the beginning by the time they start generating. Repeating the core constraint at the end keeps it in focus.
Without this pattern: You’ll get a function that uses email-validator package, returns just a boolean, and is 60 lines long.
Pattern 2: The Negative Example# Structure: Show what you DON’T want, then what you DO want.
I D t e D - - - - G O r x O e n N y c C L R D n e ' : e t a o e o e e T r p r h t g t n r d e t e i c u ' a d s t s h w r t t e o u E u : i n e r l x r s t e r t t c n p h a x t o h e e p p h r i = p { c c p o e s t " i o r s h : p i e f n o e e a r o r i t p r n o n r c e r i r d c o x i n o l e a r e t a t r i s s " x t e n s : c ( e r h g _ e e r n a d : s p e H a n f a t t q T l d o t r i u T l r a ( o e P e i ( e n s r n a i ) s t s r g n n } _ t o p i a r c A u d t o P t , u d d I ) s e e u t . e s c a n e o i d r d l p _ e s o i s i d t n ) o t . t h e c l i e n t
Why it works: AI models learn from millions of code examples, including bad ones. Showing the anti-pattern explicitly prevents the model from falling into the most common trap.
Pattern 3: The Role Lock# Structure: Assign a specific expert role with domain knowledge.
Y R o e u v i a e r w e t a h i s s e n d i a o t r a P p y r t o h c o e n s s d i e n v g e l f o u p n e c r t i w o h n o a s n p d e c s i u a g l g i e z s e t s i i m n p r h o i v g e h m - e p n e t r s f . o r m a n c e d a t a p i p e l i n e s . Y o u ' v e w o r k e d e x t e n s i v e l y w i t h A p a c h e K a f k a , R e d i s , a n d P o s t g r e S Q L . Y o u p r e f e r f u n c t i o n a l p r o g r a m m i n g p a t t e r n s a n d a l w a y s c o n s i d e r m e m o r y e f f i c i e n c y .
Why it works: The role constrains the model’s response distribution. A “senior Python developer” will suggest different things than a generic assistant — more idiomatic code, better patterns, awareness of production concerns.
Pro tip: Match the role to your actual needs. Don’t say “world’s best developer” — say “backend developer experienced with Django and PostgreSQL” for more relevant suggestions.
Pattern 4: The Chain of Files# Structure: Feed files in dependency order with clear labels.
I = [ = [ = [ T ' = p = p = p h m = a = a = a e s s s w F t F t F t b o i e i e i e u r l l l g k e c e c e c : i o o o n 1 d 2 d 3 d U g : e : e : e s ] ] ] e o m a r r n o u o s d t u a e h t w l / e i b s m s t u / i / h g u d l s d o e i e l g x n r e i p . w n i t p a . r h y r p e e e y d = . u = p ( t s = y B o e U k r = G e = n a = I s u S t c h H a e E n n R t E s i ) t c i a = l t = l i = o a n c c f e l s o s w . p r H o e t r e e c t a e r d e r t o h u e t e r s e . l e F v i a n n d t t f h i e l e i s s : s u e i n t h e m i d d l e w a r e .
Why it works: AI needs context to understand code. But dumping your entire codebase doesn’t help — it dilutes the signal. Feeding files in dependency order with clear labels gives the model a mental map of your system.
The label “BUG IS HERE” focuses attention on the right file while still providing necessary context from dependencies.
Pattern 5: The Output Template# Structure: Show the exact output format you want.
G ` c e ` l n ` a e p s r y s " d a t " e t h T " f e o e T n s e t " # [ # r # a u t s e " s e s n C t s " A e A s A s i a s t [ r t c u s e t l _ O r u t l s r c f [ n a p t e t t u o s e n r e l r c g c = t r s a e s e o e t t c n e d c s s e a a n e a u D l r t ] l l f i c i e c t o s u o n u r c l _ c l = o a n e a = t u t a t h n e m d e [ i t _ e e _ e s : d ] s d x i ( c i p f s s r s e u c e i c c n o l b o t c u f i u e t n ) n n d i t : g t ] o ( n f w [ . u h p n a a U c t r s t a e i t m o h s t n i ] h . s ) i " s " t " e e s x t a s c ] t . " f " o " r m a t :
Include tests for: happy path, zero values, negative values, boundary conditions, and invalid input types.
# * # * W S h P t y a r t u i t c t e t r u w n r o e r 6 : k : * s * : T * h B * e u i W I l i n d t c h r c o e o u m m t e p n l a t e a x t l e t m B h p u i l i n a l g t d s e , i n A I e x g p e l n i e c r i a t t e s s t e t p e s s . t s i n w h a t e v e r f o r m a t i t f e e l s l i k e — s o m e t i m e s p y t e s t , s o m e t i m e s u n i t t e s t , s o m e t i m e s b a r e f u n c t i o n s . T h e t e m p l a t e e n s u r e s c o n s i s t e n t , c o p y - p a s t e a b l e o u t p u t .
I need a REST API for a todo app. Let’s build it step by step.
Step 1: Generate just the data models (SQLAlchemy).
Todo: id, title, description, status, created_at, updated_at User: id, email, hashed_password, created_at
Wait for my approval before proceeding. T h e n a f t e r r e v i e w i n g :
Good. Step 2: Generate the CRUD operations for Todo model.
Use async SQLAlchemy Include pagination for list endpoint Include filtering by status
Wait for approval. # * * # * W A S h n P t y t a r i t u i - t c t p e t a r u w t n r o t e r e 7 : k r : * s n * : : T * * h P * * e r o A D D v s o e i k n b d i ' u e n t g g g s a i y f s n m o k g p r t f D o a o i m n r a s l , e " o n a g c t u o i c e n r o t e m e p x A l t P e , I t e a a n t p d r o o w n d h c u a e c t t p i y r o o o n u d - ' u r v c e e e a s d t y r m i e A e d P d i I . o c w r i e t h r e a s u u t l h t , s t b e e s c t a i u n s g e , t D h o e c k m e o r d , e l C I i / s C D j , u g a g n l d i n d g o c t u o m o e n m t a a n t y i o d n e " c i i s n i o o n n s e s p i r m o u m l p t t a . n e Y o o u u s ' l l y l . g B e r t e a a k i s n u g p e i r t f i i c n i t a o l s v t e e r p s s i o l n e t o s f y e o v u e r c y o t u h r i s n e g - . c o T r h r a e t c t s a e i a d r , l y t h a e n d i n g c e r t e m b e e n t t t a e l r a o p u p t r p o u a t c h a t i s e a s c l h o w s e t r a g — e . i f y o u ' r e j u s t p r o t o t y p i n g o r e x p l o r i n g a n i d e a , a s i n g l e b r o a d p r o m p t i s p e r f e c t l y f i n e .
Bug: API returns 500 error intermittently (about 1 in 20 requests).
Stack trace:
[paste stack trace]
Context:
This started after deploying commit abc123 (added connection pooling) Only happens under load (>50 req/sec) Database connections look normal in monitoring No memory leaks detected Already tried:
Increasing connection pool size (no change) Adding request timeout (no change) Rolling back connection pooling (fixes it, but we need pooling for performance) What are the most likely causes? Rank by probability.
# A A T * # - - - 📦 * # l I h T # * W l e h # [ [ [ N h T m e 1 H C * e y h s o b s Y 0 o u F w e e d e e o w r r s i v e s u A s e l t M e l t p I I o e e e n s a m r : t w t p t i C U * t o a p w r t g o s v * e r - a i o e h d e s r k P t l m r t i [ : s a t l p n n A C A * : t e t s a g I l I * * t r d l a * e n e e w s A t u C O r s f n o o s o d o n T n a g r s e d e h s u i k l i R e e h l n i s e C p a t e w k t v o R r " r e i e a i d e a a e t r t n e e v c l o s h t w i t r o i e i e n t a C T C n w c a e h r l i o a d e e a p d 2 P l y p n u s e 0 r r m ' d 2 o A t i o t e T F 6 m I r n s h a : p i c t p C a s t w e i e o t t W o d p " o d e h P r " l a p e A r i a k e v l , c c c f s : e e t ( h k l e r C u W ] o c a w h a i A ( w t * g h a l t I / i r e o t l h r p o e " G y o C e e n d k P u o s r u c n T S t d o i c o o , a i u w s e m w v M n r e p C e i g c e c a l m o s e k r m e a p M s T s , u b t g i e i o / c i i i l n o c p i g o c o H g l o l a u n t o d u l i w , u B S e s . t u o r u h - y n r C s g o r t W * l d u s u e e i * e s r E ) l v m t . s . s v ] d i p h s o e ( e l o T r r / Y w a u y h , y p o - t t o e o u p e u y a W s r s i ' n e t U o t g r d e s s m I , u e k / e p i a ] a ? t d A d p n ( i ] s o I e e y / - ( / n o p c ) ' w t p o o p t i h l t s d o — l e e h t e s p l m e s - t 1 u . w r / r s 0 b s h c e / l u E o A l c p i g v I a i u r s g e a u e r o h e r r c d w s m s y e o e - o p h t d - 2 r t e p p i c 0 - s r t a r n o 2 v e h t e g d 6 s I . e t c e / - e i t - ) c u [ o r s o t l s S b n e o i a e u v l p u b i a a . s d o s o b b - e n c u o o T 2 - r s v u h 0 c 1 i e t e 2 o 5 b t 6 d + e h i w s / e i s h p ) - P → n a e 2 R ] g a t c 0 s ( s i 2 / h d t f 6 w t y i h i / e t o f e c ) e p u f y k s ' e t . : v r w o / e e a o / n n l z a t t i l . m d r w a a e a t n a y t e d e w y o r u f s . d s o a l u n d e b e d s s , i s t n a w g t c a h k s c a . t o n c i n o n s h m g t o ) r w e a v i y e n o r t u y s o u n t s e h e ' a s t i t t p . i u * m s e h . t A h n e d o " u r t a p n u k t b f y r o p m r o " b g a e b n i e l r i i t c y " a n f d o r m c e e d s i o s c t r r e u " c t t u o r e " d s p t e h c i i n f k i i c n g a n i d n s u t s e e a f d u l o . f " a s h o t g u n l i s t o f p o s s i b i l i t i e s .