ASP.NET Hosting
ASP.NET Core hosting turns an agent runtime into HTTP endpoints for sessions, threads, content, streaming, middleware responses, and stored agent definitions.
Use hosting when a web app, TypeScript client, TUI, or another process needs to talk to HPD Agent over a boundary.
What You Are Creating
AddHPDAgentRuntime("getting-started", recipe) creates one hosted runtime. The recipe is the same AgentRuntimeBuilder chain a console app uses, including its agents and stores. Hosting builds one AgentRuntime from it when the app starts, and that runtime serves every session and thread. It can also run stored agent definitions created over HTTP; this first sample uses only the one code agent the recipe registers, assistant, which is the runtime's default agent.
The resulting identities form a hierarchy:
ASP.NET application
└── hosted runtime: getting-started
├── AgentRuntime
├── code agent assistant (default) and stored agent definitions
├── sessions
└── threadsSee Hosted Runtimes And Agents for the full model.
Create A Web App
dotnet new web -n HpdAgentHost
cd HpdAgentHost
dotnet add package HPD-Agent.AspNetCore --version 0.9.7
dotnet add package HPD-Agent.Providers.OpenAI --version 0.9.7Set an OpenAI API key:
export OPENAI_API_KEY="..."Add Program.cs
using HPD.Agent;
using HPD.Agent.AspNetCore;
using HPD.Agent.Providers.OpenAI;
var builder = WebApplication.CreateBuilder(args);
var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), ".hpd-hosting");
var definition = new AgentConfig { Name = "Getting Started Agent" }
.WithOpenAI(model: "gpt-5-mini")
.WithInstructions("You are a hosted HPD Agent. Be concise and helpful.");
builder.Services.AddHPDAgentRuntime("getting-started", runtime => runtime
.AddAgent("assistant", definition)
.WithFileSessionStore(Path.Combine(dataRoot, "sessions"))
.WithAgentStore(new JsonAgentStore(Path.Combine(dataRoot, "agents"))));
var app = builder.Build();
app.MapGet("/", () => "HPD Agent is hosted.");
app.MapGroup("/hpd").MapHPDAgent("getting-started");
app.Run();Run it:
dotnet runVerify the host is listening:
curl http://localhost:5000/Expected:
HPD Agent is hosted.Before exposing hosted routes outside local development, decide how your app will handle authentication, authorization, CORS, storage durability, rate limits, and streaming client lifecycle.
Send The First Request
The root endpoint only proves that ASP.NET started. Complete one hosted-agent flow to verify that the runtime, session, thread, and model are wired together.
Create a session:
curl -X POST http://localhost:5000/hpd/sessions \
-H "Content-Type: application/json" \
-d '{}'Copy the returned session id, then list its threads:
curl http://localhost:5000/hpd/sessions/{sessionId}/threadsCopy the thread id from the returned main thread and submit a message. The input names no agent, so the runtime's default agent, assistant, answers; add "agentId" to the body to run another agent of the runtime:
curl -X POST \
http://localhost:5000/hpd/sessions/{sessionId}/threads/{threadId}/inputs \
-H "Content-Type: application/json" \
-d '{"text":"Explain what a hosted agent is in one sentence."}'The request returns 202 Accepted. Read authoritative control state:
curl \
http://localhost:5000/hpd/sessions/{sessionId}/threads/{threadId}/stateOpen the committed event stream for transcript and live output:
curl -N \
"http://localhost:5000/hpd/sessions/{sessionId}/threads/{threadId}/events?after=1:0"You have now exercised the hosted path end to end. The deeper Hosted Streaming API explains cursor recovery, steering, interruption, and middleware responses.
What Happens
AddHPDAgentRuntime(...) registers a named hosted runtime and installs the immutable provider composition generated from the provider packages referenced by this application. A hosted service runs the recipe and calls BuildAsync() on the runtime when the app starts, and disposes the runtime when the app stops. BuildAsync() validates each code agent against that composition, so a definition that names a provider the application does not reference stops the app at startup. The agent itself is built the first time it runs.
AddAgent("assistant", definition) registers the AgentConfig as a code agent with the id assistant. As the only code agent, it is the default agent. The registration name, getting-started, selects the runtime in MapHPDAgent(...) and is not an agent id. A code agent is read-only over HTTP and never written to the agent store. WithOpenAI(...) selects its chat provider; agent settings belong on the definition.
The recipe is applied once to build the runtime. WithFileSessionStore(...) creates a segmented FileSessionStore for hosted sessions and canonical thread journals, with a file-backed content store beside it. WithAgentStore(...) stores agent definitions created over HTTP; this sample uses JsonAgentStore. Without these calls the runtime uses in-memory stores.
MapHPDAgent(...) maps the built-in HPD Agent endpoints under /hpd.
An input's agentId selects the default agent or a stored definition on the same runtime; an input without one runs the default agent. GET /hpd/agents lists both, each with its source (Code or Stored). Unknown agent ids return 404 and are never synthesized.
When a hosted input request arrives, hosting resolves the agent the input names, reserves thread-run ownership, commits the start fact, and queues the input into that runtime. The backend owns committed events, middleware responses, client tools, permissions, and interruptions. Hosted clients read control state from /state and use one /events?after={appliedCursor} SSE stream for catch-up and future commits. For the runtime and the agents it runs, see The Runtime And Its Agents.
Next
At this point, the first path is complete: define an agent, run and stream it, add a tool, keep session history, save state, and host the runtime.
Choose a next track:
| Track | Start Here |
|---|---|
| Build a web client | Hosted Streaming API and TypeScript Client Events |
| Host multiple agents | Hosted Runtimes And Agents and Stored Agent Definitions |
| Expose a workflow over HTTP | ASP.NET Multi-Agent Workflows |
| Add real tool surfaces | Author A Tool Harness, MCP Servers, and OpenAPI Tools |
| Publish a contract or client | ASP.NET OpenAPI Contracts |
| Add safety and control | Middleware Overview and Permissions Middleware |
| Add orchestration | Subagents and Multi-Agent Overview |
| Ship the runtime | ASP.NET Core Hosting, Stored Agent Definitions, and Logging And Telemetry |
For a short orchestration tutorial, continue to Build A Multi-Agent Workflow.