Generation
Connect the acting model. A policy backend generates tokens; PolicyFormat binds its tokenizer and chat template to an action parser. Backends resolve the format from the model name unless you supply one. Import these from focus.backends; build a policy step by step in Quick start: MATH.
GenerateResult
Section titled “GenerateResult”Record one model response, including generated tokens, text, probabilities, and stopping information. Backend adapters return this record to the policy, which hands it to the agent.
Definition
Fields
HermesToolCallParser
Section titled “HermesToolCallParser”Read Hermes-style JSON tool-call blocks from a model response and separate them from reasoning and visible text.
Use in PolicyFormat for a model whose chat template emits Hermes tool-call blocks.
Definition
HermesToolCallParser.parse
Section titled “HermesToolCallParser.parse”NativeToolCallParser
Section titled “NativeToolCallParser”Convert a backend’s structured tool calls into ToolTurn actions for ToolEnv.
Use with a backend that returns tool calls in structured response fields.
Definition
NativeToolCallParser.parse
Section titled “NativeToolCallParser.parse”OpenAIChatPolicy
Section titled “OpenAIChatPolicy”Run agent interactions through a Chat Completions endpoint with native message and tool-call handling. Local token accounting is estimated.
This backend uses the configured API credentials and structured tool calls for agent execution and evaluation. Exact actor-training spans require a backend that preserves exact token IDs.
Definition
OpenAIChatPolicy.init
Section titled “OpenAIChatPolicy.init”OpenAIChatPolicy.agenerate
Section titled “OpenAIChatPolicy.agenerate”OpenAIResponsesPolicy
Section titled “OpenAIResponsesPolicy”Run agent interactions through a Responses endpoint, preserving its native response and tool-call structure.
This backend uses the configured API credentials for agent execution and evaluation. Use a backend that preserves exact token IDs when collecting exact actor-training spans.
Definition
OpenAIResponsesPolicy.init
Section titled “OpenAIResponsesPolicy.init”OpenAIResponsesPolicy.agenerate
Section titled “OpenAIResponsesPolicy.agenerate”Policy
Section titled “Policy”Generate model output through a backend or a custom completion function. Its format supplies the codec and the agent’s default action parser.
policy_format determines how inputs and outputs are represented. complete returns generated tokens and their log probabilities, which remain attached to those tokens through training-data preparation. The context manager prepares each request. Keep the actor and critic fixed for each tree; the training loop owns model snapshots and batch-age checks. Several collectors can share local execution capacity while calling their own policy objects directly.
Definition
Policy.init
Section titled “Policy.init”Policy.format
Section titled “Policy.format”Policy.model
Section titled “Policy.model”Policy.served_model
Section titled “Policy.served_model”Policy.trainable
Section titled “Policy.trainable”Policy.prepare
Section titled “Policy.prepare”Policy.validate_context
Section titled “Policy.validate_context”Check an edited conversation before committing it. Native API policies check tool-call and reply ordering; context managers enforce their own prefix-preservation requirements. This check makes no model request.
Policy.agenerate
Section titled “Policy.agenerate”Policy.generate
Section titled “Policy.generate”Policy.agenerate_tokens
Section titled “Policy.agenerate_tokens”Generate directly from token IDs using the effective sampling settings. Use this path for rollout requests and replay.
Policy.generate_tokens
Section titled “Policy.generate_tokens”Policy.decode
Section titled “Policy.decode”Policy.parse
Section titled “Policy.parse”Policy.is_exact
Section titled “Policy.is_exact”Report whether this particular response preserves exact token provenance. trainable describes the policy’s capability before collection; is_exact() records what happened on an individual call, including when a replay policy mixes exact and inexact responses.
Policy.record_reply
Section titled “Policy.record_reply”Policy.startup_check
Section titled “Policy.startup_check”Check that the policy is ready to serve requests. Backends can override this hook to contact their server.
Policy.close
Section titled “Policy.close”Release policy-owned resources.
Policy.aclose
Section titled “Policy.aclose”Release resources owned by an asynchronous policy client.
PolicyFormat
Section titled “PolicyFormat”Bind a codec for encoding model input to a parser for interpreting model output.
The codec renders messages as model tokens. The parser interprets output in the model’s format and serves as the agent’s default. resolve() loads the tokenizer and selects a parser from profile, or infers a supported tool-call dialect from the model name. Profiles are qwen_xml, hermes, native, text, and token_chunk. The agent can adapt the parser for text or token-chunk actions; Agent(parser=...) selects an explicit parser. Tool environments need a parser that matches the model’s tool-call syntax. Construct PolicyFormat(name, codec, parser) directly for a custom format.
Definition
Fields
PolicyFormat.prepare
Section titled “PolicyFormat.prepare”Render the supplied conversation and tool schemas into a prompt snapshot.
PolicyFormat.record_reply
Section titled “PolicyFormat.record_reply”Append an assistant message, preserving native tool-call fields when supplied.
PolicyFormat.resolve
Section titled “PolicyFormat.resolve”The tokenizer, chat template, and parser for a known model family.
profile names the parser: "qwen_xml" (Qwen3.5 tool calls),
"hermes" (Qwen2.5 tool calls), "native" (API tool calls),
"text", or "token_chunk" (exact segments). Without one, the model
name selects the tool dialect.
PreparedPrompt
Section titled “PreparedPrompt”Store the messages, tool schemas, and tokens for one generation request. The agent’s context manager prepares it; policy.prepare(messages) renders an ordinary conversation.
Definition
Fields
PreparedPrompt.messages
Section titled “PreparedPrompt.messages”PreparedPrompt.tools
Section titled “PreparedPrompt.tools”QwenXMLToolCallParser
Section titled “QwenXMLToolCallParser”Read Qwen3.5-style XML tool calls and convert their function names and arguments into ToolTurn actions.
Use in PolicyFormat for a model whose template emits Qwen XML tool calls.
Definition
QwenXMLToolCallParser.parse
Section titled “QwenXMLToolCallParser.parse”SGLangPolicy
Section titled “SGLangPolicy”Generate token continuations through an independently hosted SGLang server, preserving their exact token IDs.
This policy uses the server’s native /generate endpoint and owns its HTTP clients. Supply the model’s format so token rendering and action parsing match the server.
Definition
SGLangPolicy.init
Section titled “SGLangPolicy.init”SGLangPolicy.generate
Section titled “SGLangPolicy.generate”SGLangPolicy.generate_tokens
Section titled “SGLangPolicy.generate_tokens”SGLangPolicy.agenerate_tokens
Section titled “SGLangPolicy.agenerate_tokens”SGLangPolicy.startup_check
Section titled “SGLangPolicy.startup_check”SGLangPolicy.close
Section titled “SGLangPolicy.close”SGLangPolicy.aclose
Section titled “SGLangPolicy.aclose”SamplingParams
Section titled “SamplingParams”Set generation controls for a request, including sampling temperature, output length, and requested token probabilities.
This request allows up to 512 generated tokens and asks for each sampled token’s log probability. Those probabilities support training and token-based allocation.
Definition
Fields
SlimePolicy
Section titled “SlimePolicy”Generate tokens through Slime’s SGLang router using the training framework’s HTTP transport.
post supplies Slime’s asynchronous transport, and url identifies the generation endpoint. Slime controls admission to that transport.
Definition
SlimePolicy.init
Section titled “SlimePolicy.init”SlimePolicy.agenerate_tokens
Section titled “SlimePolicy.agenerate_tokens”TextActionParser
Section titled “TextActionParser”Use the stripped completion text as the environment action.
The agent uses this parser for an environment that reads text, even when the policy’s format parses tool calls.
Definition
TextActionParser.parse
Section titled “TextActionParser.parse”TinkerPolicy
Section titled “TinkerPolicy”Use a Tinker sampling client as the generation backend for a rollout.
sampling_client generates from a fixed policy snapshot. policy_format supplies prompt encoding and a default action parser; the agent’s context manager prepares each request.
Definition
TinkerPolicy.init
Section titled “TinkerPolicy.init”TinkerPolicy.agenerate_tokens
Section titled “TinkerPolicy.agenerate_tokens”TinkerPolicy.close
Section titled “TinkerPolicy.close”TinkerPolicy.enter
Section titled “TinkerPolicy.enter”TinkerPolicy.exit
Section titled “TinkerPolicy.exit”TokenChunk
Section titled “TokenChunk”Carry one generated segment into an environment: the original token IDs and whether the model finished its response.
The environment appends these two tokens to its state. finished=False means generation can continue from that prefix; finished=True tells a final-answer environment to grade the completed response.
Definition
Fields
TokenChunkParser
Section titled “TokenChunkParser”Extract a generated segment’s original token IDs and whether the response has finished.
A length finish produces finished=False; a stop finish produces finished=True. Other finish reasons raise an error. Segments uses this parser and sets the generation limit; the execution service validates each response against that limit before the parser consumes it.
Definition
TokenChunkParser.parse
Section titled “TokenChunkParser.parse”TokenId
Section titled “TokenId”An integer identifying one token in the policy’s tokenizer vocabulary. Token IDs must come from the tokenizer bound to that policy.
Definition
ToolTurn
Section titled “ToolTurn”Represent a parsed assistant response as visible text, reasoning, and structured tool calls. Tool parsers produce this record and ToolEnv consumes it.
Definition
Fields
VLLMPolicy
Section titled “VLLMPolicy”Generate from token prompts through a vLLM server and retain exact token IDs and behavior log probabilities for training.
model_name identifies the model and tokenizer, and policy_format supplies the template and parser. Set served_model if the server uses a different model name. The endpoint must support vLLM’s token-completion fields.
Definition
VLLMPolicy.init
Section titled “VLLMPolicy.init”Connect to the vLLM endpoint with the supplied model format. timeout sets the request deadline in seconds; None uses the SDK default. Allow for queueing time when many requests share the server, since timeouts and retries can add work to an already busy queue.
VLLMPolicy.startup_check
Section titled “VLLMPolicy.startup_check”VLLMPolicy.generate
Section titled “VLLMPolicy.generate”VLLMPolicy.generate_tokens
Section titled “VLLMPolicy.generate_tokens”VLLMPolicy.agenerate_tokens
Section titled “VLLMPolicy.agenerate_tokens”VLLMPolicy.close
Section titled “VLLMPolicy.close”VLLMPolicy.aclose
Section titled “VLLMPolicy.aclose”ActionKind
Section titled “ActionKind”What an action is: "text" (the reply’s text), "tool" (a tool-calling turn), or "tokens" (an exact token chunk). Parsers declare the kind they produce; environments and context managers declare the kind they need, and the agent adapts the policy format’s parser to match.
Definition
NativeToolCall
Section titled “NativeToolCall”Represent a structured tool call returned by a model backend, separately from completion text.
Definition
Fields
NativePolicy
Section titled “NativePolicy”Provide the shared message validation and client lifecycle inherited by OpenAI chat and Responses policies. Instantiate the concrete backend classes to select a transport.
Definition
NativePolicy.init
Section titled “NativePolicy.init”NativePolicy.validate_context
Section titled “NativePolicy.validate_context”NativePolicy.close
Section titled “NativePolicy.close”NativePolicy.aclose
Section titled “NativePolicy.aclose”ActionParser
Section titled “ActionParser”Decode a GenerateResult into the action type consumed by Env.step(). Implement parse() and declare whether the parser supports token-level training or requires native tool calls.
Bind this parser in PolicyFormat when your environment consumes integer actions. The parser decodes the generated text; Env.step() defines what that integer does in the task.
Definition
Fields
ActionParser.parse
Section titled “ActionParser.parse”Required implementation.
Return the action result expresses (or a task-defined malformed one).
ToolCall
Section titled “ToolCall”Hold a parsed tool’s name and JSON argument string.
Definition
Fields
TOOL_CALL
Section titled “TOOL_CALL”Match serialized tool-call blocks in a model completion.
Definition
tool_turn
Section titled “tool_turn”Parse a completion into a tool-calling assistant turn using a supplied decoder for each call block.
Definition
Completion
Section titled “Completion”Specify an asynchronous completion callback: accept prefix token IDs and optional sampling parameters, then return a GenerateResult.
The callback receives the exact prefix token IDs and optional per-call sampling settings. It returns a GenerateResult asynchronously. Bind backend to the policy transport used by your application.
Definition
PostFn
Section titled “PostFn”Specify the asynchronous HTTP callback used by SlimePolicy to send generation requests.
Definition
DEFAULT_MAX_TOKENS
Section titled “DEFAULT_MAX_TOKENS”Set the output-token limit used when sampling parameters leave it unspecified.
Definition
DEFAULT_TEMPERATURE
Section titled “DEFAULT_TEMPERATURE”Set the sampling temperature used when no explicit value is supplied.
Definition
DEFAULT_TOP_P
Section titled “DEFAULT_TOP_P”Set the nucleus-sampling probability used when no explicit value is supplied.
Definition