Mastering Agentic AI with Java: Live Course
Spring AIAI Engineering

Prompt Engineering


Prompt Engineering is the practice of crafting effective prompts to obtain accurate, predictable, and specific outputs from AI language models. The quality of the response directly depends on the effectiveness of the prompt.

AI is like a very knowledgeable person who provides accurate answers when asked specific questions, but may give excessive or irrelevant information if not guided properly.


Why Prompt Engineering Matters

Bad PromptingGood Prompting
Uncertain, unpredictable outputsPredictable, specific outputs
Excessive or irrelevant informationFocused, relevant responses
Generic answersTailored, actionable results

Even as large language models improve, well-crafted prompts consistently produce better results than vague ones.


Four Components of a Good Prompt

Every effective prompt should include these key components:

Components_of_Prompt

ComponentPurposeExample
InstructionWhat you want the AI to do"Explain," "Generate," "Compare," "Summarize"
InputThe specific data or topic to work withA code snippet, a paragraph, a question
ContextBackground information to guide the responseUser's expertise level, project details, constraints
Output FormatExpected structure of the responseJSON, table, bullet points, paragraph

Example

Instruction: Compare the following frontend frameworks
Input: React, Angular, Vue
Context: I am a junior developer choosing my first framework
Output: Present as a markdown table with columns for learning curve,
        community size, and job market demand

Adding more context results in better output — a well-contextualized prompt provides the AI with necessary information to generate accurate and relevant responses.


Prompt Engineering Techniques

Zero-Shot Prompting

The simplest approach — a clear, concise instruction without any examples.

AspectDetail
How it worksRelies solely on clear instructions to guide the response
When to useSimple questions, quick answers, straightforward tasks
LimitationLeast controlled; output format may vary

Example:

What is a smart contract in blockchain?

Few-Shot Prompting

Providing examples of the desired output format along with the prompt, so the model learns the expected pattern.

AspectDetail
How it worksGive 2-3 examples of the desired output before asking the actual question
When to useWhen you need a specific format, structure, or style
BenefitGuarantees consistent, structured outputs matching the desired format

Example — Generating Commit Messages:

Generate a commit message for the following change.

Examples:
- Change: Added user authentication → Commit: feat: add user authentication with JWT tokens
- Change: Fixed navbar alignment → Commit: fix: resolve navbar alignment issue on mobile
- Change: Updated README → Commit: docs: update README with installation instructions

Now generate for:
- Change: Added login button to the homepage

Output: feat: add login button to homepage

Few-shot prompting establishes a pattern that the AI follows, ensuring consistent and predictable outputs.

Chain of Thought (CoT) Prompting

Forces the AI to break down complex problems into step-by-step reasoning, similar to showing work in mathematics.

AspectDetail
How it worksAsk the model to "think step by step" or show its reasoning
When to useComplex calculations, logical problems, multi-step reasoning
BenefitIncreases accuracy and allows verification of the logic path

Example:

A store has 45 apples. They sell 12 in the morning and receive
a shipment of 30 in the afternoon. How many apples do they have now?

Think step by step.

Output:

Step 1: Start with 45 apples
Step 2: Sell 12 → 45 - 12 = 33 apples
Step 3: Receive 30 → 33 + 30 = 63 apples
Answer: 63 apples

Adding "think step by step" slows down the model's response, improving accuracy by forcing it to reason through each stage rather than jumping to an answer.

Role Prompting

Assigning a specific persona or role to the AI, causing it to adjust technical depth, terminology, and examples to match that role's expertise.

AspectDetail
How it worksPrefix the prompt with a role assignment
When to useWhen you need responses tailored to a specific expertise level or perspective
BenefitTransforms the depth and style of responses

Example:

You are a senior blockchain architect. Explain smart contracts
to a junior developer using relatable analogies.

The AI will adjust its response to be technically informed but accessible, using analogies appropriate for a beginner.

Structured Output Prompting

Specifying the exact output format (JSON, CSV, table, list) to get parseable, predictable data.

AspectDetail
How it worksExplicitly state the desired format in the prompt
When to useAI-powered applications that need machine-readable data
BenefitPredictable, parseable output rather than free-form text

Example:

List the top 3 JavaScript frameworks with their release year
and primary use case. Return the result as JSON.

Output:

[
  { "name": "React", "year": 2013, "useCase": "UI component libraries" },
  { "name": "Angular", "year": 2016, "useCase": "Enterprise SPAs" },
  { "name": "Vue", "year": 2014, "useCase": "Progressive web apps" }
]

Structured output prompting is critical for AI-powered applications that need to parse and process AI responses programmatically.

Prompt_Technique_Comparison


Context Optimization

Embedding contextual information about the user's background, goals, and expertise level enables AI to generate tailored responses rather than generic answers.

Context ElementImpact
User's expertise levelAdjusts complexity of explanation
Project constraintsFocuses on relevant solutions
Desired outcomeAligns response with goals
Industry/domainUses appropriate terminology

Summary

  • Prompt Engineering is a critical skill for AI Engineers, the quality of output directly depends on the quality of the prompt.
  • A good prompt has four components: Instruction, Input, Context, and Output Format.
  • Zero-shot is simplest but least controlled; few-shot ensures format consistency through examples.
  • Chain of Thought improves accuracy on complex problems by forcing step-by-step reasoning.
  • Role prompting tailors responses to specific expertise levels and perspectives.
  • Structured output is essential for building AI-powered applications that need predictable, parseable data.
  • Always provide clear instructions, relevant context, and specify the expected output format for best results.

Written By: Muskan Garg

How is this guide?

Last updated on