![]() |
| https://www.pexels.com/photo/close-up-of-man-carrying-ropes-for-rock-climbing-12585932/ |
Agent Harness in Microsoft Agent Framework provides a preconfigured runtime for turning a language model into an agent capable of sustained, multi-step work. A language model can generate a response or request a tool call, but an effective agent also needs to preserve state, manage context, track progress, govern tool use, and determine how execution should continue. Agent Harness supplies this operational layer so developers do not have to assemble the entire agent loop themselves.
During a task, the Harness coordinates model and tool calls while maintaining conversation and session state. Plans and todos keep the agent oriented, context compaction controls token growth, approval policies govern tool use, and telemetry exposes what happened during execution. These capabilities help the agent retain continuity when a task cannot be completed in one response.
The Harness is not a separate runtime. It assembles existing Agent Framework components, including chat clients, pipelines, sessions, context providers, middleware, and tools. The result remains a standard Agent Framework agent with the same extension points as one assembled manually. Developers gain practical defaults while retaining the ability to disable, replace, or extend individual capabilities.
Agent Harness is not a model host, deployment service, or prescribed user interface. The application still chooses a model provider, controls where the agent runs, and presents progress or approval requests to the user.
How It Is Composed
- A chat client connects the agent to a language model.
- A chat pipeline handles function calls, message injection, history persistence, and optional context compaction.
- Context providers supply session instructions, memory, todo state, operating modes, and tools.
- Middleware and decorators add approval handling, OpenTelemetry observability, and optional bounded looping.
- The application streams responses and provides the user experience for progress updates and tool approvals.
Core Capabilities
| Capability | Behavior | | ------------------- | -------------------------------------------------------------- | | Function invocation | Runs tools with a configurable iteration limit | | History persistence | Saves history after each model call in a tool-calling run | | Todo tracking | Tracks task progress by default | | Agent modes | Provides plan and execute modes by default | | Session file memory | Provides session-scoped file memory by default | | Tool approval | Supports standing approvals and automatic approval rules | | Observability | Emits OpenTelemetry data by default | | Web search | Adds search when the selected chat client supports it | | Context compaction | Reduces context when token limits or a strategy are configured |
How This Project Uses Agent Harness
create_harness_agent. An Azure AI Foundry deployment provides the model through FoundryChatClient, authenticated by DefaultAzureCredential. A Lagom dependency-injection container supplies the chat client, logger, session store, health specialist, and medical-center lookup tool.InMemoryHistoryProvider and two tools:health_advisor is a specialist Agent Framework agent exposed through as_tool. Its prompt requires accessible general health information, explicit uncertainty, appropriate professional follow-up, and immediate emergency guidance for potentially life-threatening symptoms.medical_centers is a FunctionTool backed by a deterministic Python function. It maps supported transplant procedures to a medical center and returns a fallback when no center is configured.agent_harness.py is:agent = create_harness_agent(
client=self._client.get_client(),
agent_instructions=get_system_prompt(),
context_providers=[InMemoryHistoryProvider(load_messages=False)],
max_context_window_tokens=128_000,
max_output_tokens=8_000,
middleware=[
ToolTimingMiddleware(
statistics,
agent_tool_names={"health_advisor"},
)
],
tools=[
health_advisor.create_agent(statistics).as_tool(
name="health_advisor",
description="Answer a medical or health question.",
),
FunctionTool(
name="medical_centers",
description="Find medical center where procedure can be performed.",
func=medical_centers.run,
),
],
)Request Flow
- Start a new
TurnStatisticsmeasurement. - Run the coordinator with the current
AgentSession. - Let the coordinator respond directly when appropriate or invoke a registered tool.
- Record the duration and type of every tool invocation, even when a tool raises an exception.
- Capture token usage reported by the delegated health agent.
- Log the coordinator response metadata, delegated usage, and combined toke total before displaying the answer.
- Persist the session when the user exits or when execution terminates with an error.
TurnStatistics object connects the coordinator and specialist agent. ToolTimingMiddleware wraps coordinator tool calls and classifies each one as either an agent delegation or a function invocation. AgentUsageMiddleware, installed when the health advisor creates its agent, records usage from both regular and streamed responses. The resulting statistics include work performed by the specialist rather than reporting only the coordinator's model usage.Durable Sessions
AgentSession. At startup, the application restores a valid session or creates a new one. Invalid JSON and incompatible session data are moved to a uniquely named quarantine file, so a damaged session does not prevent the application from starting.os.replace. This atomic replacement avoids leaving a partially written primary session file if the process is interrupted during persistence. The outer finally block in the command loop ensures that the latest session is persisted during both normal exit and error handling.
Comments
Post a Comment