方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

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:

  1. System prompt — trusted instructions that go in the system role
  2. User context — structured data about the current user (preferences, metadata) injected into the system block
  3. Tool schemas — a list of tool definition dicts that will be passed to the API separately
  4. Chat history — previous conversation turns that must be trimmed to fit a token budget

Requirements

  • Accept a max_history_tokens parameter and trim conversation history from the oldest end if the history exceeds this budget
  • Use a simple token estimate: len(text.split()) * 1.3 for word-based estimation
  • Always preserve the system prompt and the most recent user message in full
  • Return a PromptAssembly dataclass with messages, estimated_tokens, and tools_count fields
  • Raise ValueError if the system prompt alone exceeds max_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

Practice stage

General drill

Hint

Keep the solution explicit and reviewable.

Success criteria

Make the solution explicit, debuggable, and easy to explain.

Review checklist

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.