Prompt Engineering Fundamentals: Ask LLMs the Right Way

This guide covers essential prompt engineering techniques for large language models, including clear instructions, role assignment, output formatting, context injection, few-shot examples, chain-of-thought reasoning, structured outputs, troubleshooting common failures, and template-based prompting for RAG and agent applications.

Code Farmer Manor Chronicle
Code Farmer Manor Chronicle
Code Farmer Manor Chronicle
Prompt Engineering Fundamentals: Ask LLMs the Right Way

What LLMs Eat: Instruction + Context + Output Format

Large language models are not search engines; they predict the next token. A clear prompt typically contains three components:

Explicit task – what to do and what not to do.

Relevant context/material – provide necessary information, avoid irrelevant data.

Output requirements – format, length, style.

Three Basic Techniques

1. Clear Instructions with Defined Boundaries

Bad: Help me write an intro. Good:

Write a product intro under 150 words for beginners, explaining usage and steps, casual tone.

2. Role Setting + Constraints

You are a senior backend architect. Explain 'cache penetration' in plain language for a Java engineer with 2 years experience, provide 2 solutions and code examples. No theory, just conclusions.

3. Structured Output Format

Convert the following text to JSON with fixed fields {name, age, city}, ignore extra info.
Text: Zhang San is 25, lives in Shanghai.

Mastering these three techniques is sufficient for daily use.

Context Injection: Feeding Reference Material

Models have knowledge cutoffs and can hallucinate. Critical facts must be supplied in the prompt:

Below is my product FAQ (authoritative source):
---
{FAQ text}
---
Answer user questions only based on the above material. If not in material, honestly say 'I don't know', do not fabricate.
User question: {user input}

This is the core idea of Retrieval-Augmented Generation (RAG): retrieve relevant content, insert it into the prompt, then let the model answer.

Few-Shot: Examples Beat Rules

Providing 2–3 input-output examples lets the model imitate the desired pattern:

Translate Chinese product names to English strings, output only translation, no explanation.

Chinese: Wireless Mouse → Output: Wireless Mouse
Chinese: Mechanical Keyboard → Output: Mechanical Keyboard
Chinese: Monitor Stand → Output:

The model learns both format and semantics from examples, which is more stable than writing ten rules.

Chain-of-Thought: Step-by-Step Reasoning

Directly asking complex reasoning often fails. Guiding the model to reason step by step improves accuracy:

Store purchases 30 apples, sells 1/3, then buys 6 more. How many now?
Please think step by step, show each calculation, then give conclusion.

Advanced: use connectives like "therefore" or "step by step" to trigger reasoning, or explicitly require "list solution steps first, then give answer".

Structured Output: Constraining Format for Programmatic Parsing

In application development, models must output JSON for downstream code. Use format constraints plus examples:

You are an order parser. Parse user message to JSON: {"action": action, "target": object, "amount": quantity}
User message: Help me order a large latte, less ice.
Output (only JSON, no extra text):

Common Failure Troubleshooting

Irrelevant answer – Cause: vague instruction. Fix: clarify task, boundaries, format.

Hallucinated facts – Cause: missing reference material. Fix: inject context, forbid fabrication.

Unstable format – Cause: no output examples. Fix: provide few-shot or JSON schema.

Wrong answer on complex problems – Cause: single-step too hard. Fix: break into CoT steps or multiple prompts.

Overly long output – Cause: no length constraint. Fix: specify ≤150 words or split into 3 points.

From Handwritten Prompts to Programmatic Templates

In AI applications (RAG/Agent), prompts become templates with dynamic injection:

# Build prompt with template, inject user input dynamically
template = """You are {role}. Answer based on the following material, say you don't know if not found:
{context}
User question: {question}
"""
prompt = template.format(
role="Customer Service Assistant",
context=retrieved_docs,
question=user_input
)

Key principle: Keep context, user input, and fixed instructions separate during assembly to prevent injection attacks (e.g., user input containing "ignore previous instructions").

One-Sentence Summary

Prompt Engineering = Explicit Task + Sufficient Material + Fixed Format + Examples/Step-by-Step when Needed.

This toolkit improves daily AI interaction quality and forms the foundation for building RAG and Agent applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LLMprompt engineeringLarge Language ModelsRAGAgentChain-of-ThoughtFew-shotstructured output
Code Farmer Manor Chronicle
Written by

Code Farmer Manor Chronicle

A heart like drifting clouds, ever at ease; a mind like flowing water, free to roam.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.