Prompt engineering e finestre di contesto
Comprendete la finestra di contesto che limita ogni chiamata LLM e imparate a creare prompt che combinino contesto recuperato, istruzioni e domande rimanendo entro i limiti, per ottenere risposte affidabili in produzione.
Prompt engineering e finestre di contesto è una lezione LLM Apps in Production (RAG + Vector DB + Caching) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento LLM Apps in Production (RAG + Vector DB + Caching), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso LLM Apps in Production (RAG + Vector DB + Caching) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Context Window
Every LLM has a fixed context window — the max tokens it reads and writes per call. System prompt, retrieved docs, history, and your question all have to fit.
What Lives in the Window
A production RAG prompt packs in system instructions, retrieved context, prior history, and the current question. Exceed the window and something gets cut.
Anatomy of a Prompt
A clear prompt structure helps the model tell instructions apart from data. Here's a clean layout separating context from the question.
prompt = (
'You are a support assistant. '
'Answer ONLY from the context.\n\n'
'Context:\n{context}\n\n'
'Question: {question}'
)Grounding Instructions
To cut hallucination, add grounding instructions: tell the model to answer only from the provided context and to admit when it doesn't know.
rule = 'If the answer is not in the context, say you do not know.'Using a Prompt Template
A prompt template makes prompts reusable and safe to fill with variables. LangChain's ChatPromptTemplate does exactly this.
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
('system', 'Answer only from context: {context}'),
('human', '{question}')
])Counting Tokens
Before sending, count tokens so you don't overflow the window. Rough rule for English: about 4 characters per token.
import tiktoken
enc = tiktoken.get_encoding('cl100k_base')
print(len(enc.encode(filled_prompt)))When Context Is Too Big
When context is too big, shrink it: fewer chunks, smaller chunk sizes, or summarize. Never silently truncate the middle — you might drop the answer.
The Lost-in-the-Middle Effect
Watch the lost-in-the-middle effect: models attend best to the start and end of context, worst to the middle. Put your most relevant chunks first or last.
Reserving Output Space
Input and output share the window. Fill it all with input and there's no room to generate — so reserve output space for the expected answer length.
max_output = 800
budget_for_input = WINDOW - max_outputFew-Shot Examples
A few few-shot examples can steer format and tone, but they eat tokens. Weigh their value against the space they consume.
Iterating on Prompts
Prompt engineering is empirical: change one thing at a time, test on real questions, and measure. Small wording tweaks can shift answer quality a lot.
Quick Check
Test your understanding of context windows.
Recap
Recap: the context window holds system, context, history, question, and output. Structure prompts, ground them, count tokens, beat lost-in-the-middle, and reserve output room.
Domande Frequenti
La lezione «Prompt engineering e finestre di contesto» è gratuita?
Sì — il testo completo di «Prompt engineering e finestre di contesto» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso LLM Apps in Production (RAG + Vector DB + Caching), passa a CoddyKit PRO. Il corso LLM Apps in Production (RAG + Vector DB + Caching) include 4 lezioni in totale.
Cosa imparerò in «Prompt engineering e finestre di contesto»?
Comprendete la finestra di contesto che limita ogni chiamata LLM e imparate a creare prompt che combinino contesto recuperato, istruzioni e domande rimanendo entro i limiti, per ottenere risposte aff… Eserciti LLM Apps in Production (RAG + Vector DB + Caching) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare LLM Apps in Production (RAG + Vector DB + Caching)?
Non è richiesta alcuna esperienza precedente. LLM Apps in Production (RAG + Vector DB + Caching) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Prompt engineering e finestre di contesto»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione LLM Apps in Production (RAG + Vector DB + Caching)?
Sì. Ogni lezione LLM Apps in Production (RAG + Vector DB + Caching) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Comprendere le app LLM in produzione
- Fondamenti della Retrieval Augmented Generation
- Panoramica dell'architettura di base di un sistema RAG
- Prompt engineering e finestre di contesto