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

Implementation:ARCANGEL0 EVA request agent turn

From Leeroopedia
Knowledge Sources
Domains LLM, API_Integration, Networking
Last Updated 2026-10-29 14:00 GMT

Overview

Concrete tool for dispatching LLM requests to OpenAI-compatible and Anthropic-native provider APIs with streaming, cancellation, and error handling, provided by the Mitsu provider client.

Description

The request_agent_turn function is the entry point for provider communication. It selects the appropriate protocol handler based on the profile's `protocol` field:

  • OpenAI protocol: Formats messages with system prompt, tool definitions, and reasoning parameters. Streams SSE events for progressive output. Supports reasoning_effort/reasoning parameters for models that support them.
  • Anthropic protocol: Formats messages with system prompt, tool_use/tool_result blocks. Streams SSE events with Anthropic-specific event types.

The function uses stdlib urllib only — no external HTTP libraries — for maximum portability. Streaming is handled via a callback (`on_event`) that receives parsed events as they arrive. A `RequestCancellation` token can close the HTTP response mid-stream if the operator interrupts.

Error handling hides API keys in error messages before raising `ProviderRequestError`.

Usage

  • Agent turn loop: Called (via a background thread wrapper) from `run_agent_turn` to get the model's response.
  • Streaming UI updates: The `on_event` callback forwards text deltas and tool call events to the TUI for real-time rendering.
  • Interruptible requests: Pass a `RequestCancellation` token to allow mid-stream cancellation.

Code Reference

Source Location

  • Repository: Mitsu
  • File: providers/client.py

Signature

def request_agent_turn(
    profile: dict[str, Any],
    messages: list[dict[str, Any]],
    tools: list[dict[str, Any]],
    on_event: StreamCallback | None = None,
    cancellation: RequestCancellation | None = None,
) -> ProviderTurn:

Import

from mitsu.providers import request_agent_turn, ProviderTurn, RequestCancellation

I/O Contract

Inputs

Name Type Required Description
profile dict[str, Any] Yes Provider config with base_url, model, api_key, protocol, reasoning settings
messages list[dict] Yes Full message list (system + history)
tools list[dict] Yes Tool definitions for the model
on_event StreamCallback No Streaming event callback
cancellation RequestCancellation No Cancellation token to close mid-stream

Outputs

Name Type Description
returns Contains text, assistant_message, tool_calls, usage, response_info

Usage Examples

Basic Non-Streaming Request

from mitsu.providers import request_agent_turn

profile = {
    "base_url": "https://api.openai.com/v1",
    "model": "gpt-4",
    "api_key": "sk-...",
    "protocol": "openai",
}

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"},
]

turn = request_agent_turn(profile, messages, tools=[])
print(turn.text)
print(f"Tool calls: {len(turn.tool_calls)}")
print(f"Usage: {turn.usage}")

Streaming with Cancellation

from mitsu.providers import request_agent_turn, RequestCancellation

cancellation = RequestCancellation()

def on_event(kind: str, data: dict):
    if kind == "text":
        print(data["text"], end="", flush=True)
    elif kind == "tool_call":
        print(f"\nTool call: {data}")

turn = request_agent_turn(
    profile,
    messages,
    tools,
    on_event=on_event,
    cancellation=cancellation,
)

# From another thread, cancel mid-stream:
# cancellation.cancel()

Related Pages

Implements Principle

Requires Environment

Page Connections

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