prompt-engineering

Original🇺🇸 English
Translated

Use when "writing prompts", "prompt optimization", "few-shot learning", "chain of thought", or asking about "RAG systems", "agent workflows", "LLM integration", "prompt templates"

13installs
Added on

NPX Install

npx skill4agent add eyadsibai/ltk prompt-engineering

Tags

Translated version includes tags in frontmatter

Prompt Engineering Guide

Effective prompts, RAG systems, and agent workflows.

When to Use

  • Optimizing LLM prompts
  • Building RAG systems
  • Designing agent workflows
  • Creating few-shot examples
  • Structuring chain-of-thought reasoning

Prompt Structure

Core Components

ComponentPurposeInclude When
Role/ContextSet expertise, personaComplex domain tasks
TaskClear instructionAlways
FormatOutput structureNeed structured output
ExamplesFew-shot learningPattern demonstration needed
ConstraintsBoundaries, rulesNeed to limit scope

Prompt Patterns

PatternUse CaseKey Concept
Chain of ThoughtComplex reasoning"Think step by step"
Few-ShotPattern learning2-5 input/output examples
Role PlayingDomain expertise"You are an expert X"
Structured OutputParsing neededSpecify JSON/format exactly
Self-ConsistencyImprove accuracyGenerate multiple, vote

Chain of Thought Variants

VariantDescriptionWhen to Use
Standard CoT"Think step by step"Math, logic problems
Zero-Shot CoTJust add "step by step"Quick reasoning boost
Structured CoTNumbered stepsComplex multi-step
Self-AskAsk sub-questionsResearch-style tasks
Tree of ThoughtExplore multiple pathsCreative/open problems
Key concept: CoT works because it forces the model to show intermediate reasoning, reducing errors in the final answer.

Few-Shot Learning

Example Selection

CriteriaWhy
RepresentativeCover common cases
DiverseShow range of inputs
Edge casesHandle boundaries
Consistent formatTeach output pattern

Number of Examples

CountTrade-off
0 (zero-shot)Less context, more creative
2-3Good balance for most tasks
5+Complex patterns, use tokens
Key concept: Examples teach format more than content. The model learns "how" to respond, not "what" facts to include.

RAG System Design

Architecture Flow

Query → Embed → Search → Retrieve → Augment Prompt → Generate

Chunking Strategies

StrategyBest ForTrade-off
Fixed sizeGeneral documentsMay split sentences
Sentence-basedPrecise retrievalMany small chunks
Paragraph-basedContext preservationMay be too large
SemanticMixed contentMore complex

Retrieval Quality Factors

FactorImpact
Chunk sizeToo small = no context, too large = noise
OverlapPrevents splitting important content
Metadata filteringNarrows search space
Re-rankingImproves relevance of top-k
Hybrid searchCombines keyword + semantic
Key concept: RAG quality depends more on retrieval quality than generation quality. Fix retrieval first.

Agent Patterns

ReAct Pattern

StepDescription
ThoughtReason about what to do
ActionCall a tool
ObservationProcess tool result
RepeatUntil task complete

Tool Design Principles

PrincipleWhy
Single purposeClear when to use
Good descriptionsModel selects correctly
Structured inputsReliable parsing
Informative outputsModel understands result
Error messagesGuide retry attempts

Prompt Optimization

Token Efficiency

TechniqueSavings
Remove redundant instructions10-30%
Use abbreviations in examples10-20%
Compress context with summaries50%+
Remove verbose explanations20-40%

Quality Improvement

TechniqueEffect
Add specific examplesReduces errors
Specify output formatEnables parsing
Include edge casesHandles boundaries
Add confidence scoringCalibrates uncertainty

Common Task Patterns

TaskKey Prompt Elements
ExtractionList fields, specify format (JSON), handle missing
ClassificationList categories, one-shot per category, single answer
SummarizationSpecify length, focus areas, format (bullets/prose)
GenerationStyle guide, length, constraints, examples
Q&AContext placement, "based only on context"

Best Practices

PracticeWhy
Be specific and explicitReduces ambiguity
Provide clear examplesShows expected format
Specify output formatEnables parsing
Test with diverse inputsFind edge cases
Iterate based on failuresTargeted improvement
Separate instructions from dataPrevent injection

Resources