StageScope

The single unified scope for the training DSL.

Provides all operations: nested stages, agent runs, prompt executions, action logging, and dataset iteration.

Stages can be nested to arbitrary depth via runStage. Leaf operations (runAgent, executePrompt) create leaf records.

When to use stages: stages are primarily for exception handling, metric aggregation, and structured logging. If a piece of code doesn't need any of these, it doesn't need to be wrapped in a stage. A good "smell" that you're using something wrong is if your stage only has a single substage inside: while technically okay, you will have unnecessary metrics aggregation in the records tree, polluting it.

Inheritors

Properties

Link copied to clipboard

The full training dataset, used as the default for iterateDataset.

Link copied to clipboard
abstract val trackedAgent: GraphAIAgent<Input, Output>

The agent being trained, available as the default target of runAgent.

Functions

Link copied to clipboard
suspend fun StageScope<*, *, *>.executePrompt(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): Result<List<Message.Response>>

Executes an LLM prompt with consumption tracking. Creates a PromptExecutionRecord as a leaf record, recording elapsed time and consumption. On success, returns the raw LLM response messages.

Link copied to clipboard
suspend fun StageScope<*, *, *>.executePromptOrThrow(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): List<Message.Response>

.getOrThrow() shortcut over executePrompt; throws on a recorded execution failure.

Link copied to clipboard
inline suspend fun <T> StageScope<*, *, *>.executePromptStructured(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): Result<T>

Executes a structured LLM prompt with consumption tracking. Creates a PromptExecutionRecord and returns the deserialized structure.

Link copied to clipboard
inline suspend fun <T> StageScope<*, *, *>.executePromptStructuredOrThrow(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): T

.getOrThrow() shortcut over executePromptStructured; throws on a recorded execution failure.

Link copied to clipboard
abstract suspend fun <T> executeWithTrackedPromptExecutor(name: String, retryPolicy: RetryPolicy? = null, execute: suspend CapturingPromptExecutor.() -> T): Result<T>

Helper function that creates a consumption-capturing executor, runs execute with it, records the result as a PromptExecutionRecord, and recomputes metrics. Notice that this function only produces one record, so you should only make a single LLM call inside the execute block.

Link copied to clipboard
abstract suspend fun iterateDataset(name: String = "Dataset iteration", dataset: TrainSet<Input, InputLabel> = this@StageScope.dataset, customMetricsToRecord: List<Metric>? = null, failureRateThreshold: Double = 0.9, earlyStop: (TrainSetItem<Input, InputLabel>) -> PrematureExecutionStopDecision = { PrematureExecutionStopDecision(false) { "Unreachable: this training never stops prematurely" } }, processItem: suspend StageScope<Input, Output, InputLabel>.(TrainSetItem<Input, InputLabel>) -> Unit): StageRecord

Iterates over the dataset, processing each item as a substage. Uses dataset-specific metrics (solved rate, item counts, agent run stats).

Link copied to clipboard
abstract fun logAction(json: Json = defaultExperimentsJson, builder: ActionLogBuilder.() -> Unit)

Builds an action log via the ActionLogBuilder DSL and stores it on the current stage record.

Link copied to clipboard

Convenience wrapper that maps a collection, enclosing it in a dedicated stage. Pros: automatically sets substagesTotal to enable ETC calculation.

Link copied to clipboard
abstract fun recordCustomMetric(metric: Metric)

Attaches a custom metric to the records tree of this stage. This method has two uses:

Link copied to clipboard
abstract suspend fun runAgent(item: TrainSetItem<Input, InputLabel>, agentToRun: GraphAIAgent<Input, Output> = trackedAgent, retryPolicy: RetryPolicy? = null): Result<CompletedAgentRun<Output>>

Runs the agent on the given item.

Link copied to clipboard
suspend fun <Input, Output, InputLabel> StageScope<Input, Output, InputLabel>.runAgentOrThrow(item: TrainSetItem<Input, InputLabel>, agentToRun: GraphAIAgent<Input, Output> = trackedAgent, retryPolicy: RetryPolicy? = null): CompletedAgentRun<Output>

.getOrThrow() shortcut over StageScope.runAgent; throws on a recorded run failure.

Link copied to clipboard
abstract suspend fun runAgentWithRetries(item: TrainSetItem<Input, InputLabel>, maxAttempts: Int, until: RunUntil = RunUntil.SOLVED, agentToRun: GraphAIAgent<Input, Output> = trackedAgent, innerRetryPolicy: RetryPolicy? = null): CompletedAgentRun<Output>?

Re-samples the agent: runs it up to maxAttempts times, returning the first attempt whose outcome satisfies until (default RunUntil.SOLVED). The intended use is intentional re-sampling — e.g. bootstrap demonstration generation — not transient-failure recovery.

abstract suspend fun <RunData> runAgentWithRetries(item: TrainSetItem<Input, InputLabel>, maxAttempts: Int, until: RunUntil = RunUntil.SOLVED, innerRetryPolicy: RetryPolicy? = null, agentProvider: suspend () -> PreparedAgentRun<Input, Output, RunData>): MatchedAgentRun<Output, RunData>?

Runs the agent up to maxAttempts times, creating a fresh agent per attempt via agentProvider.

Link copied to clipboard
suspend fun <Input, Output, InputLabel, T> StageScope<Input, Output, InputLabel>.runIterableStage(collection: Collection<T>, name: String, itemBlock: suspend StageScope<Input, Output, InputLabel>.(T) -> Unit): Result<Unit>

Convenience wrapper that iterates a collection, enclosing it in a dedicated stage. Pros: automatically sets substagesTotal to enable ETC calculation.

Link copied to clipboard
suspend fun <Input, Output, InputLabel, T> StageScope<Input, Output, InputLabel>.runIterableStageOrThrow(collection: Collection<T>, name: String, itemBlock: suspend StageScope<Input, Output, InputLabel>.(T) -> Unit)
Link copied to clipboard
abstract suspend fun <T> runStage(name: String, substagesTotal: Int? = null, metrics: MetricsMap = standardStageMetrics(), block: suspend StageScope<Input, Output, InputLabel>.() -> T): Result<T>

Runs a named substage. Creates a StageRecord as a substage of the current stage.

Link copied to clipboard
suspend fun <Input, Output, InputLabel, T> StageScope<Input, Output, InputLabel>.runStageOrThrow(name: String, substagesTotal: Int? = null, metrics: MetricsMap = standardStageMetrics(), block: suspend StageScope<Input, Output, InputLabel>.() -> T): T

.getOrThrow() shortcut over StageScope.runStage. See its KDoc for the metrics preset conventions; the default and parameter semantics are identical.

Link copied to clipboard
inline fun <T> StageScope<*, *, *>.setAdditionalData(data: T)

Serializes data with defaultExperimentsJson and stores it as this stage's additional data via StageScope.setAdditionalDataJson. Overwrites any previously set value.

Link copied to clipboard
abstract fun setAdditionalDataJson(additionalData: JsonElement)

Sets some additional data in this stage's record. Overwrites if additional data was already added.