AI Engineer Portal
Your personal operating system for career transition.
Exercise
Build a prompt assembly pipeline
Build a Prompt Assembly Pipeline
Every LLM feature you ship assembles a messages array before making an API call. Doing this carelessly — concatenating strings, ignoring token counts, mixing trusted and untrusted content — is one of the most common sources of production bugs.
What you are building
Create an assemble_messages function that combines four components into a properly structured messages array:
- System prompt — trusted instructions that go in the
systemrole - User context — structured data about the current user (preferences, metadata) injected into the system block
- Tool schemas — a list of tool definition dicts that will be passed to the API separately
- Chat history — previous conversation turns that must be trimmed to fit a token budget
Requirements
- Accept a
max_history_tokensparameter and trim conversation history from the oldest end if the history exceeds this budget - Use a simple token estimate:
len(text.split()) * 1.3for word-based estimation - Always preserve the system prompt and the most recent user message in full
- Return a
PromptAssemblydataclass withmessages,estimated_tokens, andtools_countfields - Raise
ValueErrorif the system prompt alone exceedsmax_history_tokens
Why this matters
Context budget management is the difference between a feature that works reliably in production and one that throws mysterious 400 errors when users have long conversations. Building this discipline in early pays dividends across every LLM feature you ship.
Llm Foundations / easy / Step 1 of 6
General drill
Keep the solution explicit and reviewable.
Make the solution explicit, debuggable, and easy to explain.
Review where the boundary is, what gets validated, and what would be hard to debug later.
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.