/generate endpoint is the most direct interface; you control tokenization. The
OpenAI-format /v1/chat/completions endpoint is router-session aware and fits
agent loops with multi-turn dialogue.
Both entry points are wired up through
--custom-generate-function-path.
The /generate endpoint
What generate_hub is
miles/rollout/generate_hub/ ships reusable generate functions that conform to the
refactored rollout interface (GenerateFnInput / GenerateFnOutput). They compose
with custom agents, tool use, or multi-turn logic.
Key modules:
Generate function basics
The runtime contract:- The rollout engine passes a
GenerateFnInputcontaining:state: tokenizer, processor, args, sampling defaults.sample: the prompt, current tokens, response, status.sampling_params:max_new_tokens,temperature,top_p, etc.
- Your function:
- Builds a request from the prompt.
- Executes it against SGLang.
- Updates the
Samplewith tokens, logprobs, loss mask, status.
compute_prompt_ids_from_sampleandcompute_request_payloadfrommiles/rollout/generate_utils/generate_endpoint_utils.pybuild/generaterequests.- A generate function can set
GenerateFnOutput.samplesto aSampleorlist[Sample].
Reference generators
single_turn.py: single-turn generation via/generate. Text or multimodal prompts.multi_turn.py: multi-turn tool calling via/generate. Adds CLI flags--generate-max-turns,--generate-tool-specs-path,--generate-tool-call-parser,--generate-execute-tool-function-path.benchmarkers.py: forces random output sequence length for benchmarking.
The OpenAI chat endpoint
Minimal run_agent
A run_agent receives a session-scoped base_url. Send OpenAI-format chat requests
to base_url/v1/chat/completions and pass the messages list as the prompt.
OpenAI chat messages
Standard OpenAI format:Quickstart
Generator entry point:miles/rollout/generate_hub/agentic_tool_call.py: OpenAI-format agent loop via router sessions.
examples/swe-agent: multi-turn agentic SWE agent on the session-server TITO path, with ready-to-run launchers.
Optional teardown: the abort hook
The module named by --custom-agent-function-path may expose an optional abort
function alongside the agent entry point:
max_seq_len or timeout. If your
agent drives an external backend (e.g. a sandbox/agent server), define abort to
tell that backend to tear down the trials tied to this rollout.
The hook is entirely optional and safe to omit:
- If the module defines no
abort, nothing is called — existing plugins are unaffected and their in-flight generations simply drain as before. - It only fires when
--custom-agent-function-pathis set, so non-agentic runs never invoke it.
swe_agent_function.abort
for a reference implementation that flushes the Harbor agent server.
Customizing the wrapper
agentic_tool_call.generate
is a thin wrapper around the custom agent. It:
- Creates a session on MilesRouter and builds a session-scoped
base_url. - Calls the custom agent (from
--custom-agent-function-path) to send one or more chat requests. - Collects server-assembled
Sampleobjects viaOpenAIEndpointTracer.collect_samples(the session server converts records into samples, truncates and merges on the owning instance; records never leave the server).
/generate path above.
TITO (token-in / token-out)
TITO needs two things from every SGLang response:- Prompt token ids: extracted from
response.choices[0].prompt_token_ids. Returned when the request setsreturn_prompt_token_ids=True. - Output token ids and logprobs: from
response.choices[0].logprobs.content[*](token_id,logprob). Returned whenlogprobs=True.
build_chat_request_kwargs sets both flags. The session middleware
forwards raw messages to SGLang, which tokenizes the prompt and returns the
response. _compute_sample_from_openai_record in
merge.py
extracts prompt and output ids from the response and concatenates them into
sample.tokens. You don’t need to provide input_ids yourself.
Multi-turn samples can be saved within a single session, but tokens are not
inherited across turns. Each request is tokenized independently.
Common pitfalls
Next
- Customization: the full catalog of
--*-pathhooks. - Agentic Chat Templates: verifying that a template is append-only across turns.
- Multi-agent example: full agentic walkthrough.

