Setup
I am running a Microsoft Foundry Hosted Agent using a custom Python container and the Invocations protocol. The agent calls a GPT-4.1-mini deployment through the Foundry project endpoint:
POST https://<account>.services.ai.azure.com/api/projects/<project>/openai/v1/chat/completions
The OpenAI client is obtained through AIProjectClient.get_openai_client().
Authentication uses DefaultAzureCredential, which resolves to ManagedIdentityCredential inside the hosted container, with the scope https://ai.azure.com/.default.
The dependencies are pinned and identical across all runs:
azure-identity 1.26.0
azure-ai-projects 2.8.0
openai 3.31.0
langchain-openai 1.7.2
azure-ai-agentserver-core 2.2.0
azure-ai-agentserver-invocations 1.2.0
SDK retries are disabled.
Problem: Intermittent HTTP 401 PermissionDenied
I performed three diagnostic runs:
1. Version A – First run (failed)
The first chat/completions request returned HTTP 401 with error code PermissionDenied.
At that time, the Hosted Agent identity only had the implicit project access described in Microsoft's documentation.
2. Version A – Second run (successful)
I assigned the built-in Foundry User role to the agent identity at project scope.
Approximately 30 minutes later, the same Hosted Agent version and container image successfully completed four GPT-4.1-mini requests.
3. Version B – Third run (failed)
Approximately 16 hours later, I created a new Hosted Agent version with application-code changes.
The following remained unchanged:
Agent identity and project-scoped role assignments
Model authentication code
SDK versions and dependencies
Token scope and project endpoint
Environment variables
Nevertheless, the first chat/completions request again returned HTTP 401 PermissionDenied.
What I have verified
Both authenticated startup calls to my backend API succeeded in the third run. The backend recorded the expected agent identity as the caller.
The explicit project-scoped Foundry User role assignment is still present, unconditional and unchanged.
The project's managed identity still has Foundry User at the Foundry account scope.
Azure Activity Log shows no relevant RBAC, identity, account, project or model-deployment changes between the successful and failed runs.
Foundry account metrics recorded four successful model requests during the second run, but zero model requests during the two failed runs.
The failed runs also show zero client errors in account-level metrics, suggesting the requests were rejected before reaching the model deployment.
The model authentication implementation and installed SDK dependencies are identical between Versions A and B.
Every run was a cold start. In the third run, the managed identity token was acquired milliseconds before the rejected request.
I have not captured the claims of the actual ai.azure.com token rejected by the model endpoint, and I cannot access the gateway-side authorization decision.
Therefore, I cannot yet determine why the request was rejected.
Questions
Does /api/projects/<project>/openai/v1/chat/completions require permissions beyond the documented implicit Hosted Agent project access or an explicit project-scoped Foundry User role? Does the documented responses/* permission also cover chat/completions?
Can creating a new Hosted Agent version or session change the effective identity or authorization context used for outbound model requests, even when the configured agent identity and permissions remain unchanged?
Could gateway-side authorization caching, or re-evaluation when a new version is created, explain why a project-scoped assignment that worked for Version A is not honoured for Version B?
What diagnostics would you recommend to identify the rejected token's principal and audience, and the gateway's authorization decision, without exposing access tokens or assigning broader permissions? Is there a diagnostic-log category or another supported method for investigating denied project-endpoint requests?
I can share the relevant UTC timestamps and provider request IDs privately with a Microsoft engineer.
I would appreciate guidance on identifying which component returns PermissionDenied and how to resolve the issue without unnecessarily broadening RBAC permissions.
Setup
I am running a Microsoft Foundry Hosted Agent using a custom Python container and the Invocations protocol. The agent calls a GPT-4.1-mini deployment through the Foundry project endpoint:
POST https://<account>.services.ai.azure.com/api/projects/<project>/openai/v1/chat/completions
The OpenAI client is obtained through AIProjectClient.get_openai_client().
Authentication uses DefaultAzureCredential, which resolves to ManagedIdentityCredential inside the hosted container, with the scope https://ai.azure.com/.default.
The dependencies are pinned and identical across all runs:
azure-identity 1.26.0
azure-ai-projects 2.8.0
openai 3.31.0
langchain-openai 1.7.2
azure-ai-agentserver-core 2.2.0
azure-ai-agentserver-invocations 1.2.0
SDK retries are disabled.
Problem: Intermittent HTTP 401 PermissionDenied
I performed three diagnostic runs:
1. Version A – First run (failed)
The first chat/completions request returned HTTP 401 with error code PermissionDenied.
At that time, the Hosted Agent identity only had the implicit project access described in Microsoft's documentation.
2. Version A – Second run (successful)
I assigned the built-in Foundry User role to the agent identity at project scope.
Approximately 30 minutes later, the same Hosted Agent version and container image successfully completed four GPT-4.1-mini requests.
3. Version B – Third run (failed)
Approximately 16 hours later, I created a new Hosted Agent version with application-code changes.
The following remained unchanged:
Agent identity and project-scoped role assignments
Model authentication code
SDK versions and dependencies
Token scope and project endpoint
Environment variables
Nevertheless, the first chat/completions request again returned HTTP 401 PermissionDenied.
What I have verified
Both authenticated startup calls to my backend API succeeded in the third run. The backend recorded the expected agent identity as the caller.
The explicit project-scoped Foundry User role assignment is still present, unconditional and unchanged.
The project's managed identity still has Foundry User at the Foundry account scope.
Azure Activity Log shows no relevant RBAC, identity, account, project or model-deployment changes between the successful and failed runs.
Foundry account metrics recorded four successful model requests during the second run, but zero model requests during the two failed runs.
The failed runs also show zero client errors in account-level metrics, suggesting the requests were rejected before reaching the model deployment.
The model authentication implementation and installed SDK dependencies are identical between Versions A and B.
Every run was a cold start. In the third run, the managed identity token was acquired milliseconds before the rejected request.
I have not captured the claims of the actual ai.azure.com token rejected by the model endpoint, and I cannot access the gateway-side authorization decision.
Therefore, I cannot yet determine why the request was rejected.
Questions
Does /api/projects/<project>/openai/v1/chat/completions require permissions beyond the documented implicit Hosted Agent project access or an explicit project-scoped Foundry User role? Does the documented responses/* permission also cover chat/completions?
Can creating a new Hosted Agent version or session change the effective identity or authorization context used for outbound model requests, even when the configured agent identity and permissions remain unchanged?
Could gateway-side authorization caching, or re-evaluation when a new version is created, explain why a project-scoped assignment that worked for Version A is not honoured for Version B?
What diagnostics would you recommend to identify the rejected token's principal and audience, and the gateway's authorization decision, without exposing access tokens or assigning broader permissions? Is there a diagnostic-log category or another supported method for investigating denied project-endpoint requests?
I can share the relevant UTC timestamps and provider request IDs privately with a Microsoft engineer.
I would appreciate guidance on identifying which component returns PermissionDenied and how to resolve the issue without unnecessarily broadening RBAC permissions.