Context engineering is the deliberate selection, budgeting, and ordering of information for each model call. A long-running assistant must fit conversation history alongside instructions, tool definitions, retrieved documents, and space for a response. What fits—and what can be omitted—depends on the application and the request. section 4.7 section 4.8
Context engineering is broader than prompt engineering. Prompt engineering writes the model’s instructions; context engineering decides what else enters the window and when. The surrounding agent loop, tool routing, retrieval, and safeguards form the harness that puts those decisions into operation. section 10.9
A practical assembly policy makes several decisions explicit:
- Separate stable instructions from request-time data. Keep capabilities, behavior rules, and safety boundaries in a versioned system prompt; assemble memory, documents, and history separately. section 10.8
- Assign priorities and budgets. Reserve space for essential inputs, then divide the remainder according to the application’s needs. Define a degradation strategy for each source when space is tight. section 10.14 section 10.8
- Choose history strategies deliberately. Truncation retains recent messages but can lose critical early facts. Summaries compress older exchanges; hierarchical memory combines structured facts, summaries, and recent verbatim messages. section 4.8
- Retrieve selectively. Search past sessions when a request calls for earlier context, and allocate retrieval space against current conversation needs. Arrange important material with attention to the model’s tendency to favor the beginning and end of the window. section 4.7.6 section 4.8
Patient intake
An allergy mentioned early in a long conversation should not disappear merely because recent scheduling and insurance messages fill the history budget. Store the allergy as a structured fact, then choose which symptoms, questions, and explanations to retain from the conversation. section 4.7
Documents versus history
A knowledge-grounded assistant allocates 40% of its remaining budget to retrieved documents and gives conversation history the rest, trimming older history when necessary. An assistant whose primary job depends on conversational continuity might make the opposite trade-off. section 10.8
Listing 10.15 Context assembly: the context engineering core
def assemble_context(
user_id: str,
session_id: str,
message: str,
token_budget: int = 120000 #A
) -> list:
"""Assemble the full messages list within a token budget."""
budget = TokenBudget(total=token_budget) #B
# 1. System prompt (fixed cost, always included)
system_prompt = platform.models.get_prompt("claw-assistant")
budget.reserve(
"system_prompt",
estimate_tokens(system_prompt["content"])
)
# 2. Current message (always included)
budget.reserve("current_message", estimate_tokens(message))
# 3. Tool definitions (always included)
model_tools, _ = platform.tools.build_model_tools(
namespace="claw"
)
budget.reserve("tools", estimate_tokens(str(model_tools)))
# 4. User memory (high priority, small footprint)
user_memory = platform.sessions.get_memory(user_id=user_id)
memory_text = "\n".join(
[f"- {key}: {value}"
for key, value in user_memory.items()]
)
budget.reserve("user_memory", estimate_tokens(memory_text))
# 5. Retrieved documents (medium priority, variable size)
remaining_for_docs = budget.remaining() * 0.4 #C
relevant_docs = platform.data.search(
index_name="company-knowledge",
query=message,
top_k=5,
score_threshold=0.7
)
doc_text = format_docs_within_budget(
relevant_docs,
int(remaining_for_docs)
)
budget.reserve("documents", estimate_tokens(doc_text))
# 6. Conversation history (fills remaining space)
history, _ = platform.sessions.get_messages(
session_id=session_id
)
trimmed_history = trim_history_to_budget(
history,
int(budget.remaining())
)
# Assemble into the final messages list #D
return [ #D
{"role": "system", #D
"content": system_prompt["content"]}, #D
{"role": "system", #D
"content": ( #D
f"User memory:\n{memory_text}\n\n" #D
f"Retrieved documents:\n{doc_text}" #D
)}, #D
*[{"role": m.role, "content": m.content} #D
for m in trimmed_history], #D
{"role": "user", "content": message} #D
] #DInspect a context assembly interface that reserves budget for the system prompt, current message, and tool definitions before adding other sources. section 10.8
Listing 4.24 Strategy selection in get_messages
messages, _ = platform.sessions.get_messages(
session_id,
limit=20,
strategy="truncate" #A
)
messages, _ = platform.sessions.get_messages(
session_id,
strategy="hierarchical", #B
token_budget=15000
)
past_context = platform.data.search( #C
index_name="patient_conversations",
query=user_message,
metadata_filters={"user_id": user_id}
)
Compare SDK calls for truncation, hierarchical history, and cross-session search. section 4.7.6






