Foundation~30 minHazelJS
AgentRuntime Mental Model
How AgentRuntime owns context assembly, tool validation, budgets, checkpoints, and stop reasons—step by step for beginners inside a HazelJS app.
- Authors
- editorial-team
- Published
- Last reviewed
Direct answer
AgentRuntime is the kernel of HazelJS Agent OS. On each execute(agentName, goal, options) it owns:
assemble context → model proposes tool or final answer → validate args → optional approval → execute via ToolRegistry → append observation → optional checkpoint → stop
The LLM never calls your APIs directly. That separation is the whole point.
Docs: Agent package, Agent OS. Glossary: /glossary/agent-runtime.
Section 1 — Picture the kernel
Think of AgentRuntime as an air-traffic controller:
- The model suggests a flight plan (“call
lookupOrder”) - The runtime checks the plan against rules (schema, policy, budgets)
- Your tool code is the airplane that actually flies
- The observation is the landing report for the next suggestion
If the model could call APIs itself, you would lose validation, approvals, redaction, and audit.
Section 2 — Where it lives in your app
Agents should be normal citizens of your HazelJS DI / HTTP app — same process as your API — not a separate “agent island” that reinvents auth and logging.
import { AgentModule, AgentRuntime } from '@hazeljs/agent';
// Typical production wiring:
// AgentModule.forRoot({ runtime: { llmProvider, policyEngine, … } })
// Inject AgentRuntime into a controller or service
const result = await runtime.execute('ops-desk', goal, {
maxSteps: 8,
loop: {
maxIterations: 2,
stages: ['plan', 'execute', 'critique', 'validate'],
},
});
Starters such as hazeljs-agent-os-starter show this wiring with Inspector. Prefer that path when you leave script-level learning.
Section 3 — One step, expanded (memorize this table)
| Phase | Owner | What a beginner should notice | Failure if skipped |
|---|---|---|---|
| Context assembly | Runtime (+ memory/RAG flags) | Only budgeted text reaches the model | Hallucinated state |
| Model proposal | Provider via runtime | Proposes tool or final text | Unbounded free-form side effects |
| Arg validation | Tool contract / executor | Bad args never hit your API | Corrupt production calls |
| Policy / approval | PolicyEngine, durableSuspend | Writes can wait for a human | Irreversible damage |
| Tool execution | Host ToolRegistry / Skillgate / MCP | Your code runs with your credentials | Model-owned secrets |
| Observation | Runtime | Result becomes next context | Blind next step |
| Checkpoint | Durable run store | Mid-run state survives crash | Unsafe resume / lost work |
| Stop | maxSteps, budgets, contract | Reason is recorded | Cost incidents / mystery failures |
Section 4 — Inner loop vs outer confidence loop
Inner loop: think → (maybe) act → observe → repeat until final answer or maxSteps.
Outer loop (options.loop): plan → execute → critique → validate until successScore or maxIterations.
Use the outer loop when quality needs a critique pass. Do not use it as a substitute for good tool contracts. Pattern: /patterns/evaluator-optimizer.
Section 5 — Stop reasons you must care about
Operators cannot tune what they cannot see. Persist reasons such as:
- success / completed
- max steps reached
- budget exhausted
- policy denied
- cancelled
- suspended (waiting for human)
- contract / SLO failed
If logs only say “failed,” you cannot improve autonomy safely.
Section 6 — Worked story: ORD-1001 status
execute('ops-desk', 'Status of ORD-1001?')- Runtime builds messages from system prompt + goal
- Model proposes
lookupOrder({ orderId: 'ORD-1001' }) - Runtime validates parameters
- Your method returns
{ ok: true, status: 'shipped', … } - Runtime appends observation
- Model returns a short final answer
- Runtime stops with success; you log
executionIdfor the timeline
If step 4 fails validation, the tool never runs — and that is good.
Section 7 — Mental model checklist
- Goal is an input; done is checkable.
- Tools are typed contracts with a side-effect class.
- Budgets and
maxStepsare hard stops. - Durability is optional until the process can die mid-write — then mandatory.
- DNA/manifest describes desired identity; TypeScript still implements tools.
- The model proposes; the host executes.
Section 8 — What you will build next
You now have the vocabulary. The Agentic Development track turns it into a continuous Ops Desk build: scaffold → tool loop → budgets → context → HITL → tests → observability → CLI → checklist.
Checkpoint
- You can narrate one
executeturn without looking at notes - You know why the model must not hold production credentials
- You can name at least four stop reasons
What to do next
- Start building: /learn/agentic-development/scaffold-your-first-agent
- Hands-on lab: /guides/build-your-first-tool-using-agent
- Durability pattern: /patterns/durable-agent-run
Artifact: Annotated diagram notes of one AgentRuntime.execute turn with ToolRegistry + PolicyEngine