You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: agent-framework/agents/middleware/agent-vs-run-scope.md
+41-6Lines changed: 41 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ When both are registered, agent-level middleware runs first (outermost), followe
20
20
21
21
:::zone pivot="programming-language-csharp"
22
22
23
-
In C#, middleware is registered on an agent using the builder pattern. Agent-level middleware is applied during agent construction, while run-level middleware can be provided via `AgentRunOptions`.
23
+
In C#, middleware is registered on an agent using the builder pattern with `.AsBuilder().Use(...).Build()`. Agent-level middleware is applied during agent construction and persists across all runs. Run-level middleware uses the same pattern but builds a decorated agent inline before calling `RunAsync` or `RunStreamingAsync`.
24
24
25
25
### Agent-level middleware
26
26
@@ -30,6 +30,7 @@ Agent-level middleware is registered at construction time and applies to every r
> The `.AsBuilder().Use(...).Build()` pattern creates a lightweight wrapper around the original agent. You can chain multiple `.Use()` calls to compose several middleware for a single invocation.
> The `AddSource` method is used to specify the source name which the provider will listen to. Make sure it matches the source name you used in your instrumentation code (e.g., `UseOpenTelemetry(sourceName: SourceName)`). If a source name is not specified in the instrumentation code, it will default to `Experimental.Microsoft.Agents.AI`, in which case you should use `AddSource("Experimental.Microsoft.Agents.AI")` in your tracer provider and meter provider configuration.
93
+
91
94
> [!TIP]
92
95
> Depending on your backend, you can use different exporters. For more information, see the [OpenTelemetry .NET documentation](https://opentelemetry.io/docs/instrumentation/net/exporters/). For local development, consider using the [Aspire Dashboard](#aspire-dashboard).
93
96
@@ -112,7 +115,6 @@ var resourceBuilder = ResourceBuilder
We do *not* install exporters by default to prevent unnecessary dependencies and potential issues with auto instrumentation. There is a large variety of exporters available for different backends, so you can choose the ones that best fit your needs.
185
187
186
188
Some common exporters you may want to install based on your needs:
189
+
187
190
- For gRPC protocol support: install `opentelemetry-exporter-otlp-proto-grpc`
188
191
- For HTTP protocol support: install `opentelemetry-exporter-otlp-proto-http`
189
192
- For Azure Application Insights: install `azure-monitor-opentelemetry`
190
193
191
194
Use the [OpenTelemetry Registry](https://opentelemetry.io/ecosystem/registry/?language=python&component=instrumentation) to find more exporters and instrumentation packages.
192
195
193
196
## Enable Observability (Python)
197
+
194
198
### Five patterns for configuring observability
195
199
196
200
We've identified multiple ways to configure observability in your application, depending on your needs:
@@ -330,6 +334,7 @@ The following environment variables control Agent Framework observability:
330
334
The `configure_otel_providers()` function automatically reads standard OpenTelemetry environment variables:
@@ -356,7 +362,8 @@ Make sure you have your Foundry configured with a Azure Monitor instance, see [d
356
362
pip install azure-monitor-opentelemetry
357
363
```
358
364
359
-
#### Configure observability directly from the `AzureAIClient`:
365
+
#### Configure observability directly from the `AzureAIClient`
366
+
360
367
For Foundry projects, you can configure observability directly from the `AzureAIClient`:
361
368
362
369
```python
@@ -377,8 +384,8 @@ async def main():
377
384
> [!TIP]
378
385
> The arguments for `client.configure_azure_monitor()` are passed through to the underlying `configure_azure_monitor()` function from the `azure-monitor-opentelemetry` package, see [documentation](/python/api/overview/azure/monitor-opentelemetry-readme#usage) for details, we take care of setting the connection string and resource.
379
386
387
+
#### Configure azure monitor and optionally enable instrumentation
380
388
381
-
#### Configure azure monitor and optionally enable instrumentation:
382
389
For non-Foundry projects with Application Insights, make sure you setup a custom agent in Foundry, see [details](/azure/ai-foundry/control-plane/register-custom-agent).
383
390
384
391
Then run your agent with the same _OpenTelemetry agent ID_ as registered in Foundry, and configure azure monitor as follows:
> When agents use approval-required tools, `RequestInfoEvent` typically carries a `ToolApprovalRequestContent` payload for tool calls that require human approval. See [Human-in-the-Loop](./human-in-the-loop.md) for details on handling these events.
WorkflowEvent.type =="request_info"# A request is issued
82
85
```
83
86
87
+
> [!NOTE]
88
+
> When agents use approval-required tools, `request_info` events typically carry a `Content` payload with `type == "function_approval_request"` for tool calls that require human approval. See [Human-in-the-Loop](./human-in-the-loop.md) for details on handling these events.
| HITL with Agent Orchestrations | ✅ | ✅ | No zone pivots; links to orchestration docs |
20
21
| Checkpoints and Requests | ✅ | ✅ | |
21
22
| Next Steps | ✅ | ✅ | |
22
23
-->
@@ -230,6 +231,19 @@ while pending_responses is not None:
230
231
231
232
::: zone-end
232
233
234
+
## Human-in-the-Loop with Agent Orchestrations
235
+
236
+
The `RequestPort` pattern described above works with custom executors and `WorkflowBuilder`. When using **agent orchestrations** (such as sequential, concurrent, or group chat workflows), **tool approval** is achieved through the human-in-the-loop request/response mechanism.
237
+
238
+
Agents can use tools that require human approval before execution. When the agent attempts to call an approval-required tool, the workflow pauses and emits a `RequestInfoEvent` just like the `RequestPort` pattern, but the event payload contains a `ToolApprovalRequestContent` (C#) or a `Content` with `type == "function_approval_request"` (Python) instead of a custom request type.
239
+
240
+
> [!TIP]
241
+
> For complete examples with code, see:
242
+
> -[Sequential orchestration with HITL](./orchestrations/sequential.md#sequential-orchestration-with-human-in-the-loop)
|[Group Chat](group-chat.md)| Agents collaborate in a shared conversation |
21
21
|[Magentic](magentic.md)| A manager agent dynamically coordinates specialized agents |
22
22
23
+
> [!TIP]
24
+
> Orchestrations support **human-in-the-loop** interactions through tool approval and request info. Agents can use approval-required tools that pause the workflow for human review before execution. See [Human-in-the-Loop](../human-in-the-loop.md) and the [sequential orchestration HITL tutorial](sequential.md#sequential-orchestration-with-human-in-the-loop) for details.
0 commit comments