Prompt Engineering: A Technical Guide to Getting More From LLMs


Prompt engineering is the practice of crafting inputs to large language models (LLMs) to reliably produce accurate, high-quality outputs. While it sounds like just “writing better instructions,” at a technical level it involves understanding how models process context and how phrasing shapes token probability distributions.

This guide covers the four most impactful techniques, with concrete examples.


Why Prompt Structure Matters at the Model/Token Level

LLMs generate text by predicting the next most probable token given the full context window. Your prompt isn’t interpreted semantically the way a human reads it — it’s tokenized, embedded, and attended to as a sequence. This has direct practical consequences:

  • Word order matters. Instructions placed near the end of a prompt often receive higher attention weight than those buried in the middle.
  • Specificity collapses ambiguity. Vague prompts produce high-entropy outputs — many plausible continuations. Precise prompts narrow the token distribution toward your target output.
  • Examples shift the prior. Few-shot examples work because they establish a pattern in the context that the model continues probabilistically.

Understanding this helps you move from “why isn’t this working?” to deliberate, testable changes.


1. Few-Shot Prompting

Zero-shot prompting gives the model a task with no examples. Few-shot prompting provides labeled examples before the target input — and it’s significantly more reliable for nuanced tasks.

Classify the sentiment of each review.

Review: "Incredible build quality, very fast."  →  Positive
Review: "Stopped working after two weeks."      →  Negative
Review: "It's fine, nothing special."           →  Neutral

Review: "Great battery, but the screen is dim." →

Use 3–5 examples, cover edge cases, and keep them balanced across classes. The examples constrain the output format and clarify the task schema — reducing the model’s output entropy toward what you actually want.


2. Chain-of-Thought (CoT) Prompting

For reasoning tasks — math, logic, code analysis — models often fail when jumping directly to an answer. Forcing a reasoning trace first dramatically improves accuracy.

Simply appending "Let's think step by step" to your prompt has been shown in published research to meaningfully improve performance on reasoning benchmarks, even in zero-shot form:

A train travels 120 miles in 2 hours, then 90 miles in 1.5 hours.
What is the average speed for the entire journey? Let's think step by step:

Why it works: the intermediate reasoning tokens act as scaffolding — the correct final answer becomes far more probable given a correct reasoning chain than from the raw question alone.


3. Structured Output Prompting

For production use, free-form prose is hard to work with programmatically. Instruct the model to return JSON directly:

Extract these fields from the job posting and return as JSON:
- job_title, company, required_skills (array), salary_range (null if missing)

Posting: "Hiring a Senior Data Engineer at Acme Corp.
Skills: Spark, dbt, Airflow. Salary: $140k–$170k."

Always provide a schema or example JSON in the prompt to prevent hallucinated keys. Use null explicitly for missing fields to enforce consistent structure. For high-reliability needs, use API-level JSON mode or function calling — these constrain generation at the decoding layer rather than relying on instruction-following.


4. Negative Prompting and Constraints

Telling the model what not to do is as important as telling it what to do. Models default to heavily trained patterns: padding, hedging language, unnecessary caveats.

Write a product description for a mechanical keyboard.
Do not use "revolutionary" or "game-changing."
No call to action. Under 80 words.

For complex constraint sets, use an explicit checklist at the end of your prompt. This triggers a self-review pass — effectively a lightweight CoT for quality control.


The Core Principle

Prompt engineering is about reducing ambiguity and constraining the output space. These techniques are composable — most production prompts combine several of them. Treat your prompts like code: version them, test them across diverse inputs, and iterate on failure modes.


Explore more tutorials on RAG pipelines, LLM evaluation, and AI agents at Aichaser.cloud.

generated with Agentic.AI

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *