Skip to content

Hosted Runtimes And Agents ​

A hosted runtime is the deployment and lifecycle boundary for HPD agents in ASP.NET Core. An agent definition is one identity and configuration inside that boundary.

This distinction matters because one ASP.NET application can host several runtimes, and one runtime can run many agents.

Hierarchy ​

text
ASP.NET application
└── hosted runtime: support
    ├── AgentRuntime: built once from the recipe
    ├── stores configured by the recipe
    ├── agent: assistant (code, default)
    ├── agent: planner (code)
    ├── agent: researcher (stored)
    ├── agent: writer (stored)
    └── session: workspace-42
        ├── thread: main
        └── thread: alternative-review

Hosted Runtime ​

A hosted runtime is created by AddHPDAgentRuntime(...):

csharp
var assistant = new AgentConfig()
    .WithOpenAI(model: "gpt-5-mini")
    .WithInstructions("You are a helpful assistant.");

var planner = new AgentConfig()
    .WithOpenAI(model: "gpt-5-mini")
    .WithInstructions("You break requests into steps.");

builder.Services.AddHPDAgentRuntime("support", runtime => runtime
    .WithFileSessionStore("App_Data/support/sessions")
    .WithAgentStore(new JsonAgentStore("App_Data/support/agents"))
    .AddAgent("assistant", assistant, isDefault: true)
    .AddAgent("planner", planner));

The Action<AgentRuntimeBuilder> is the runtime recipe: the same AgentRuntimeBuilder chain a console app uses for its agents, stores, secrets, permissions, sandbox, and shared middleware. Hosting applies the application's generated composition and service provider, runs the recipe once, and calls BuildAsync() to build the runtime's one AgentRuntime. Agent settings such as providers, instructions, and harnesses belong on each agent's AgentConfig.

The name (support) is the runtime's key: MapHPDAgent("support") and the other mapping methods select the runtime by it, and its services are keyed by it. It is not an agent id. The unnamed overload, AddHPDAgentRuntime(runtime => ...), hosts the runtime that MapHPDAgent() maps.

The runtime owns:

  • one AgentRuntime, resolved in-process as HPDAgentHost.Runtime
  • the agent store containing stored definitions
  • the session store containing sessions and thread journals
  • the content store
  • input and event compositions
  • provider and runtime policy shared by its agents
  • the HTTP authorization and route-prefix boundary configured when mapping it

A recipe without store calls gets in-memory session, agent, and content stores that the runtime owns. Runtimes registered under different names have separate agents and stores unless the application deliberately passes the same store objects to each recipe.

Agent Definition ​

An agent definition is an id, an AgentConfig, and optional metadata. The display name is the config's name. A stored definition is created with a request such as:

json
{
  "agentId": "researcher",
  "config": {
    "name": "Researcher",
    "systemInstructions": "Research evidence and cite sources."
  }
}

The definition is not a conversation and is not a running agent. The hosted runtime runs it when an input names its agentId.

Code agents are the agents the recipe registers with AddAgent(...) and harness subagents declared in code. They are read-only over HTTP and are never written to the agent store. Additional definitions are created through POST /agents.

The Default Agent ​

The default agent runs inputs that name no agent. One registered code agent is the default unless another is marked isDefault: true; with several, exactly one is marked. A recipe that registers no code agent hosts a runtime that serves only stored agents:

csharp
builder.Services.AddHPDAgentRuntime("studio", runtime => runtime
    .WithFileSessionStore("App_Data/studio/sessions")
    .WithAgentStore(new JsonAgentStore("App_Data/studio/agents")));

That runtime has no default agent, so every input names its agent. Over HTTP, an input or fork that names none returns 400 with the validation code AgentIdRequired.

Runtime Instance ​

The AgentRuntime is the executable object. Each hosted runtime has one. A hosted service builds it when the host starts and disposes it when the host stops, so shared middleware start and stop hooks run once per process. Each agent is built from its definition the first time it runs, then cached.

That one runtime serves every session and thread. Different threads run concurrently; one thread runs one input at a time. An input that names another agent id runs that agent on the same runtime, with the same stores and the same per-thread execution owner. Runtime state such as run queues and live event bridges belongs to the runtime, not to the reusable definition.

The recipe is applied once, to the runtime. Its shared middleware, stores, secrets, and provider authentication reach every agent the runtime runs; definition settings such as WithOpenAI(...) belong to the definition they are called on. Nothing in the recipe is written back to the stored definitions.

Session And Thread ​

A session is a persistence-capable workspace that can contain several threads. It is restart-durable when the runtime is configured with a durable session store. A thread is one conversation path inside that session. A thread has no agent of its own: each input names the agent that runs it, or runs the default agent, and each execution records the agent that ran it.

Use one runtime for agents that share infrastructure but have different roles. They share the runtime's routes, and an input selects one by id:

json
{ "text": "Review the draft.", "agentId": "reviewer" }

Use separate runtimes when the agents need different storage, security policy, route boundaries, or operational ownership:

csharp
builder.Services.AddHPDAgentRuntime("internal", runtime => runtime
    .AddAgent("assistant", internalAgent)
    .WithFileSessionStore("App_Data/internal/sessions"));
builder.Services.AddHPDAgentRuntime("customer", runtime => runtime
    .AddAgent("assistant", customerAgent)
    .WithFileSessionStore("App_Data/customer/sessions"));

app.MapGroup("/api/internal").MapHPDAgentRuntimeApi("internal");
app.MapGroup("/api/customer").MapHPDAgentRuntimeApi("customer");

Each runtime has its own assistant; agent ids are scoped to the runtime that registers them.

There is no fixed small limit on the number of hosted runtimes. Treat each one as an isolation and resource-management boundary rather than a synonym for one agent.

Built for production .NET agent applications.