Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1Panel dev MaxKB LLM Streaming

From Leeroopedia
Knowledge Sources
Domains LLM, Streaming
Last Updated 2026-10-10 00:00 GMT

Overview

A generation pattern that invokes an LLM with streaming output, delivering tokens incrementally while extracting reasoning content and tracking token usage.

Description

LLM streaming is the execution pattern for MaxKB's chat steps. The BaseChatStep calls `chat_model.stream(message_list)` to get an iterator of AIMessageChunk objects, then the event_content generator yields each chunk as an SSE frame. Key features include:

  • Directly return optimization — If a paragraph's similarity exceeds its `directly_return_similarity` threshold, the paragraph content is returned without LLM invocation
  • Reasoning content extraction — For models that support chain-of-thought, configurable start/end tags (default ` didSelectStart`/`</think>`) separate reasoning from answer content
  • MCP/tool integration — When tools are configured, the step delegates to `mcp_response_generator()` for agent-style tool calling
  • Token counting — After completion, `get_num_tokens_from_messages()` and `get_num_tokens()` track usage for the chat record
  • No-model fallback — Returns a localized error message if no model is configured

Usage

Use this principle when designing:

  • Real-time LLM chat interfaces with token-by-token output
  • Streaming with reasoning content (chain-of-thought) support
  • Direct-return optimizations for high-confidence knowledge hits

Theoretical Basis

The streaming pattern processes tokens as they arrive from the LLM:

# Abstract algorithm description (NOT real implementation)
def stream_llm(chat_model, messages, reasoning_tags):
    reasoning = Reasoning(start_tag, end_tag)
    full_text = ""
    for chunk in chat_model.stream(messages):
        content = reasoning.extract_content(chunk)
        reasoning_content = reasoning.extract_reasoning(chunk)
        full_text += content
        yield sse_frame(content, reasoning_content)
    # Final frame with token usage
    tokens_in = chat_model.count_tokens(messages)
    tokens_out = chat_model.count_tokens(full_text)
    yield sse_end_frame(tokens_in, tokens_out)

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment