> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/main-components/middleware/run-control.md).

# RunControlMiddleware (Internal)

Internal middleware used by AiAgent cancel and steer operations per thread

Class: `bxModules.bxai.models.middleware.core.RunControlMiddleware`

This middleware is internal infrastructure for `AiAgent.cancelRun()` and `AiAgent.steerRun()`. It is auto-attached by `AiAgent` and is not intended for direct user construction.

## Purpose

* Tracks a `CancellationToken` per in-flight thread ID
* Applies cancellation and steer input at LLM/tool checkpoints
* Cleans token registry safely when runs complete

## Public Behavior Surface

You normally use the agent API, not this middleware directly:

```javascript
agent.cancelRun( threadId: "thread-123", reason: "Cancelled by operator" )
agent.steerRun( threadId: "thread-123", message: "Use concise answers and no tools" )
```

## Hooks Used

* `beforeAgentRun` (creates token and injects into run options)
* `beforeLLMCall` (checks cancel/steer)
* `beforeToolCall` (checks cancel/steer)
* `afterAgentRun` (compare-and-swap style token cleanup)

## Internal Methods

* `cancel(threadId, reason)`
* `steer(threadId, input)`

Both return boolean indicating whether a currently active run token existed for that thread.

## Notes

* Cancellation and steering are checkpoint-based, not mid-token interruption.
* If there is no active run for a thread ID, cancel/steer are safe no-ops.
* Token cleanup uses guarded remove semantics so concurrent runs sharing a thread ID do not evict each other incorrectly.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai.ortusbooks.com/main-components/middleware/run-control.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
