liveBook · Manning

LlmRequest

As explained in Build an AI Agent (From Scratch)

LlmRequest is a structured container for a single language-model request, including instructions, conversation contents, available tools, and tool-selection settings.

LlmRequest is the outbound container for one language-model call. An agent keeps its full execution state in ExecutionContext, then selects and formats the information the model needs into an LlmRequest. The request is a staging area, not a copy of the entire context. section 4.2.2

Its four fields separate the decisions involved in a call:

  • instructions: a list of system-prompt fragments, which can combine base instructions with task-specific or dynamic guidance. section 4.5.2
  • contents: conversation history as ContentItem objects, including messages, requested ToolCalls, and ToolResults. section 4.5.2
  • tools: available BaseTool instances whose definitions the client sends to the model. section 4.5.2
  • tool_choice: an optional selection setting; "auto" lets the model decide whether to use tools, while "required" forces tool use. section 4.5.2
Listing 4.9 LlmRequest class
class LlmRequest(BaseModel):
    """Request object for LLM calls."""
    instructions: List[str] = Field(default_factory=list)
    contents: List[ContentItem] = Field(default_factory=list)
    tools: List[BaseTool] = Field(default_factory=list)
    tool_choice: Optional[str] = None

The Pydantic definition shows the four fields directly: the three lists have empty-list defaults, and tool_choice defaults to None. section 4.5.2

The agent builds the request; LlmClient handles API formatting. In a basic think–act step, _prepare_llm_request() flattens the context’s events into contents, adds the agent’s instructions and tools, and chooses "auto" when tools are available. The client then converts those items into API messages and tool definitions, makes the call through LiteLLM, and returns an LlmResponse. LlmRequest itself contains no ExecutionContext or Events reference and knows nothing about provider message formats. section 4.6.3 section 4.5.4

A minimal call

Create LlmRequest(instructions=["You are a helpful assistant."], contents=[Message(role="user", content="What is 2 + 2?")]), then pass it to await client.generate(request). The returned response holds output as ContentItem objects; this example reads an answer of "4" from a Message. section 4.5.5

Keeping request preparation separate makes context selection changeable without altering stored history or API code. The basic implementation sends every event, but the preparation step could instead summarize old messages, omit irrelevant tool results, or inject additional information. Conversion still needs care: when contents contain a ToolCall, LlmClient attaches it to the preceding assistant message in the API format and links later ToolResults by call ID. section 4.6.3 section 4.5.4

How Build an AI Agent (From Scratch) covers it

How deep it goes
Defines typed fields and defaults, then traces request preparation and client conversion; defers richer memory and context-management strategies.
Examples
Shows a Pydantic class, an event-flattening preparation method, and a request asking an assistant to calculate 2 + 2.
What it assumes
Uses Python, Pydantic, ContentItem, and BaseTool while explicitly defining the request fields and how the agent fills them.
What you can do after
Build it from scratch as a typed request container and use it to select what an agent sends to an LLM.
How it connects
Connects ExecutionContext and context engineering to LlmClient, LlmResponse, tool calling, and the agent think–act cycle.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.