liveBook · Manning

LlmResponse

As explained in Build an AI Agent (From Scratch)

LlmResponse is a structured representation of a language-model response, including generated content, tool calls, errors, and usage information.

LlmResponse is the inbound boundary of an LLM communication layer. It converts provider-specific results into a stable structure that an agent can process without scattering translation logic through Agent. section 4.5.1 section 4.5.3

A response carries three kinds of information:

  • content stores ContentItem objects, including assistant Messages, ToolCalls, or both in one response. section 4.5.3 section 4.5.4
  • error_message records network failures, rate limits, and invalid requests so the agent can retry or inform the user instead of crashing. section 4.5.3 section 4.5.4
  • usage_metadata stores input and output token counts for cost management and debugging; detailed monitoring is deferred. section 4.5.3

Agent response flow

An LlmRequest containing a user Message reaches LlmClient.generate(). The client calls the provider, parses returned text into a Message or tool requests into ToolCalls, and returns an LlmResponse that the agent iterates to extract the answer or execute tools. section 4.5.4 section 4.5.5

Listing 4.10 LlmResponse class
class LlmResponse(BaseModel):
    """Response object from LLM calls."""
    content: List[ContentItem] = Field(default_factory=list)
    error_message: Optional[str] = None
    usage_metadata: Dict[str, Any] = Field(default_factory=dict)

Inspect the minimal Pydantic model: content defaults to an empty list, while errors and usage metadata remain optional or empty. section 4.5.3

How Build an AI Agent (From Scratch) covers it

How deep it goes
Defines fields and parsing behavior for text, tool calls, errors, and token counts, while deferring detailed monitoring and cost tracking.
Examples
Uses assistant messages, mixed text-and-tool responses, captured API failures, token metadata, and a simple arithmetic request.
What it assumes
Assumes LlmRequest, LlmClient, and ContentItem types, while teaching provider-independent response handling from the communication boundary.
What you can do after
Apply it via LlmClient.generate() to consume normalized answers, dispatch ToolCalls, handle captured errors, and inspect token usage.
How it connects
Connects LlmRequest, LlmClient, ContentItem, Message, ToolCall, ToolResult, and LiteLLM within an agent communication layer.

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.