AI-Assisted Development

Prompt Engineering: The Art of Asking the Right Questions

Two developers use ChatGPT for the same task. One gets perfect code in 2 minutes. The other gets garbage after an hour of frustration. The difference? Not AI - prompts. Prompt engineering is the skill that determines productivity with AI.

  • **'Write an API'** vs **'Write a REST API on FastAPI with JWT auth, rate limiting, and OpenAPI docs'**
  • **'Make it faster'** vs **'Current complexity is O(n²). Optimize to O(n log n) using...'**
  • **Role matters:** 'As a security expert, find vulnerabilities' gets a different result than just 'check the code'
  • **Iterations:** The first answer is rarely ideal. Refine, build on context.

Предварительные знания

  • AI Interaction Paradigms: Intern, Colleague, or Expert?

GIGO: Quality In = Quality Out

**Garbage In, Garbage Out (GIGO)** - a fundamental computing principle. For AI it's critical: prompt quality determines answer quality.

**Good prompt formula:** `Context + Task + Constraints + Format = Quality Answer`

AI doesn't read minds. It works with what is written. Everything left implicit will be inferred randomly.

**Typical mistake:** Assuming AI 'understands' context. AI doesn't know the project, the requirements, or the infrastructure. Everything important must be stated explicitly.

Prompt: 'Implement authentication'. What's wrong?

Context Is Everything

**Context** is information about the environment where the code will be used. The more precise the context, the more relevant the answer.

**Four layers of context:** 1. **Technical**: language, framework, versions, dependencies 2. **Architectural**: how the code fits into the system 3. **Business**: why it's needed, what problem it solves 4. **Constraints**: performance, security, compatibility

What to specifyExampleWhy it matters
Language and versionPython 3.11Syntax differs
FrameworkFastAPIDifferent patterns
DependenciesSQLAlchemy 2.0API changes between versions
Target environmentAWS LambdaConstraints
Data volume1M recordsPerformance
Security levelPCI DSSRequirements

When asking AI to write an API endpoint, what context is CRITICAL to include?

Structured Prompts

A chaotic prompt → a chaotic answer. A **structured prompt** helps organize thinking and helps AI understand the task.

**The CRISP template:** **C**ontext - Where will this be used? **R**ole - What role should AI play? **I**nstructions - What exactly needs to be done? **S**pecifications - What are the requirements/constraints? **P**references - Format, style, examples?

**Alternative templates:**

  • **RACE**: Role, Action, Context, Expectations
  • **STAR**: Situation, Task, Action, Result
  • **Chain of Thought**: 'Think step by step, explaining each step'

**Don't overdo it:** Templates help, but don't turn a prompt into a bureaucratic form. For simple questions, context and a clear task are enough.

Which CRISP element is most often skipped, even though it's critical?

Iterative Refinement

The first prompt rarely produces a perfect result. **Iterative refinement** is the normal way to work with AI.

**Refinement strategy:** 1. Start with a base prompt 2. Evaluate the answer: what's good, what isn't? 3. Refine a specific aspect 4. Repeat until the goal is reached

**Refinement techniques:**

  • **'Change X to Y'** - targeted fix
  • **'Add handling for case Z'** - extension
  • **'Explain why A was chosen instead of B'** - understanding
  • **'Rewrite accounting for constraint C'** - refactoring
  • **'What could go wrong?'** - edge case review

**Important:** Don't start over every time there's a problem. AI remembers the conversation context. Build on what came before, clarify, correct.

You need to write a perfect prompt on the first try

Good results come through iteration

Even experts rarely get the ideal on the first attempt. Iteration is normal. AI remembers context - that's an advantage worth using.

AI generated a function, but it doesn't handle null. What's the best way to refine?

Key Takeaways

  • **GIGO:** Prompt quality = answer quality
  • **Context is critical:** Language, framework, constraints, business requirements
  • **CRISP template:** Context, Role, Instructions, Specifications, Preferences
  • **Iterations are normal:** Build on what came before, refine specific aspects
  • **AI remembers context:** Use this for effective refinement

What's Next?

Prompts are the foundation. Next comes applying them to specific scenarios:

  • Code Review with AI — How to formulate prompts for code review
  • AI for Architecture — Prompts for discussing trade-offs
  • Debugging with AI — How to describe bugs for effective assistance

Вопросы для размышления

  • Consider a recently failed prompt. What context was missing?
  • Apply the CRISP template to a current task. What changes in the result?
  • How would a developer explain the task to a new teammate? That explanation is a good prompt.

Связанные уроки

  • ml-01-intro
Prompt Engineering: The Art of Asking the Right Questions

0

1

Sign In