Prompt Engineering und Context Windows
Verstehen Sie das Context Window, das jeden LLM-Aufruf begrenzt, und lernen Sie, Prompts zu formulieren, in denen abgerufener Kontext, Anweisungen und Fragen gemeinsam Platz finden, damit zuverlässige Antworten für die Produktion entstehen.
Prompt Engineering und Context Windows ist eine kostenlose LLM Apps in Production (RAG + Vector DB + Caching)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des LLM Apps in Production (RAG + Vector DB + Caching)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der LLM Apps in Production (RAG + Vector DB + Caching)-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Prompt Engineering und Context Windows“ kostenlos?
Ja — der vollständige Text von „Prompt Engineering und Context Windows“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des LLM Apps in Production (RAG + Vector DB + Caching)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der LLM Apps in Production (RAG + Vector DB + Caching)-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Prompt Engineering und Context Windows“?
Verstehen Sie das Context Window, das jeden LLM-Aufruf begrenzt, und lernen Sie, Prompts zu formulieren, in denen abgerufener Kontext, Anweisungen und Fragen gemeinsam Platz finden, damit zuverlässig… Du übst LLM Apps in Production (RAG + Vector DB + Caching) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um LLM Apps in Production (RAG + Vector DB + Caching) zu starten?
Keine Vorkenntnisse erforderlich. LLM Apps in Production (RAG + Vector DB + Caching) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Prompt Engineering und Context Windows“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser LLM Apps in Production (RAG + Vector DB + Caching)-Lektion Code schreiben und ausführen?
Ja. Jede LLM Apps in Production (RAG + Vector DB + Caching)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- LLM-Anwendungen in der Produktion verstehen
- Grundlagen der Retrieval-Augmented Generation
- Überblick über die grundlegende RAG-Systemarchitektur
- Prompt Engineering und Context Windows