0Pricing
LLM Apps in Production (RAG + Vector DB + Caching) · 课时

提示工程与上下文窗口

理解限制每次 LLM 调用的上下文窗口,并学习如何编写提示,使检索到的上下文、指令和问题能够共同适配,从而在生产环境中获得可靠答案。

提示工程与上下文窗口 是 CoddyKit 上的免费 LLM Apps in Production (RAG + Vector DB + Caching) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LLM Apps in Production (RAG + Vector DB + Caching) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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_output

Few-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.

常见问题解答

「提示工程与上下文窗口」课时是免费的吗?

是的 — 「提示工程与上下文窗口」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LLM Apps in Production (RAG + Vector DB + Caching) 课程的其余内容,请升级到 CoddyKit PRO。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

「提示工程与上下文窗口」这节课中我会学到什么?

理解限制每次 LLM 调用的上下文窗口,并学习如何编写提示,使检索到的上下文、指令和问题能够共同适配,从而在生产环境中获得可靠答案。 你通过在浏览器中直接运行的动手代码来练习 LLM Apps in Production (RAG + Vector DB + Caching),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LLM Apps in Production (RAG + Vector DB + Caching) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LLM Apps in Production (RAG + Vector DB + Caching) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「提示工程与上下文窗口」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LLM Apps in Production (RAG + Vector DB + Caching) 课中编写并运行代码吗?

能。每节 LLM Apps in Production (RAG + Vector DB + Caching) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 了解生产环境中的 LLM 应用
  2. 检索增强生成基础
  3. RAG 系统架构概览
  4. 提示工程与上下文窗口
← 返回 LLM Apps in Production (RAG + Vector DB + Caching)