5.9 KiB
litellm-host defines typed calls, execution hooks, host services, stream delivery and the resumable machine. HTTP and Python drivers interpret the same suspension protocol in their own runtimes
| Responsibility | Shared contract | HTTP | Python |
|---|---|---|---|
| Input and output conversion | Protocol::{Request, Response, StreamHead, Chunk, Error} |
Typed input; ResponseEncoder and StreamEncoder produce HTTP values |
PythonBinding decodes prepared arguments, encodes public values and maps native errors |
| Host services | Protocol::HostCall, HostServices::call |
host-native::services::HostCallHandler answers typed calls; () handles protocols without host calls |
PythonHostCalls invokes retained Python objects; it may share an owner with the binding |
| Route-facing ops | Interceptors::{before_provider_request, result_ready, after_provider_response}, awaited through the machine channel |
host-native::Driver answers them from one NativeHooks value |
The Python driver answers them from its CallHooks<PythonRuntime> chain |
| Hooks | hooks::NativeHooks: awaited where the signature returns a result, on_event where it does not |
NativeChain composes built-ins; ObservationSender is one that queues events |
PythonCallHooks also prepares arguments, transforms public responses and receives stream callbacks; Rust built-ins join the same chain |
| Runtime driving | Machine, HostRequest::{HostCall, Intercept, Stream} |
Body polling controls demand | Native polling and inline caller-task Python awaits control progress |
hosted_call(request, execute) starts with a typed request. Its route closure receives HostServices and ChannelInterceptors and returns CallOutput. A route never carries a hook or observer of its own: every boundary it reports goes through the channel, and the driver on the other side decides who hears it. Hosted-call plumbing alone forwards the returned stream through demand replies. Lower-level CallMachine users receive a CallContext containing separately named services, interceptors and stream delivery
Core route constructors prepare their dependencies and return a closure accepting the typed request. Python starts that closure only after argument preparation, preflight and decoding succeed. These steps remain inside the driver's terminal and error handling. Decoding may retain objects for subsequent host service calls
Each driver owns terminal dispatch and reports Started, one terminal event and Cancelled through NativeHooks::on_event. HTTP observes success after response conversion or stream exhaustion, failure on errors, and cancellation on body drop. Python preserves exception identity and maps native failures once. Explicit Python stream close reports success for delivered chunks; cancellation stops further Python callback dispatch and reaches Rust hooks through CallHooks::on_cancelled
interceptors.rs owns Interceptors, the route-facing ops, and its request/response payload types. lifecycle.rs owns CallEvent, ExecutionEvent, timing, failure origin, and the observe_call / observe_unary guards a driver wraps a call in. Event payloads are generic so a runtime can retain its own response, exception and raw-response references without introducing a language dependency. snapshot() projects them into the owned event a NativeHooks sees without retaining runtime objects
hooks.rs owns the call-stage interface and its runtime-associated types, plus NativeHooks: the runtime-neutral hooks a Rust built-in implements and a native driver runs. Its signature is the delivery contract. before_provider_request and result_ready return a result, so the call waits for them; on_event returns nothing and cannot fail, so a driver may deliver it inline, queue it or spawn it. native_chain.rs composes built-ins as an onion, and litellm-host-python adapts the same hooks into its chain. It contains no Python types or legacy callback policy. A runtime supplies its context and continuation representation through HookRuntime
protocol.rs owns Protocol and suspension messages, including InterceptRequest and StreamDelivery. call.rs owns neutral operation identifiers, route outputs and their adaptation into a hosted machine. hooks.rs names shared call boundaries; consumers supply their route and callback policy. Rust service handling belongs in host-native::services; coroutine channel handles stay in machine/context.rs
Rust handlers answer suspensions through litellm-host-native::Driver, which litellm-host-http and litellm_host_native::in_process share. in_process::Host is an assembly of services, hooks and stream consumer. It is not a trait mirroring every suspension. Use run_hosted to preserve the distinction between stream completion and detachment
Keep API policy in gateway-inference and python-bridge, and legacy callback policy in callbacks-legacy-python. Python bindings and hooks expose retained references through PythonOwned, with idempotent close and GC traversal. Runtime machinery stays in driver, native, handle and runtime modules
Whether a hook is waited for is a property of the boundary, not of the hook's type. observation.rs keeps ObservationSender, a NativeHooks whose on_event queues owned snapshots on a bounded channel, never waits for delivery and counts events dropped when the queue is full or closed; the host owns receiver processing and draining. Legacy Python callbacks retain their existing awaited, fallible behavior through the Python adapter, so in that runtime even notifications wait
ExecutionFacts and ResultSource describe execution without pricing or budget policy. Interceptors::result_ready delivers these facts through an awaited InterceptRequest::ResultReady; a driver answers it with NativeHooks::result_ready after reporting the matching ExecutionEvent::ResultReady through on_event. Accounting must implement result_ready rather than read the lossy event