How can I use the Agents service through an API key?

André 40 Reputation points
2025-02-18T12:40:01.4633333+00:00

I used the basic template at the agent quickstart page, set the project to work with API keys and then picked up the keys from my agent-ai-service inside of my Resource Group.

Screenshot from 2025-02-18 09-19-37

Then I use the Azure OpenAI Service endpoint along with the Python OpenAI SDK example...

https://learn.microsoft.com/en-us/azure/ai-services/agents/quickstart?view=azure-python-preview&pivots=programming-language-python-openai

But what I end up getting isn't an Agent at all, I end up creating an Assistant and the page where I land even suggests creating the new Agents instead.

01

Sure enough I thought the problem could be with the SDK since it uses the beta.asssistants resource, but I double checked the doc over the quickstart and if the lib had an agent equivalent, but I found nothing.

I eventually managed to create an Agent through the API by not utilizing the OpenAI SDK and using the Azure SDK instead, but with it I only managed to login through the connectrion string and the CLI and found no way to use an API Key instead.

So, is there a way that I could use the Azure SDK along with the API Key? Or is there a mistake in the OpenAI SDK example since it's creating assistants instead of agents?

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,085 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,604 questions
{count} votes

Accepted answer
  1. Sina Salam 22,031 Reputation points Volunteer Moderator
    2025-02-19T13:57:01.63+00:00

    Hello André,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to use the Agents service through an API key.

    To use Azure AI Agents with API keys:

    The first thing is to authenticate using the Azure SDK with your endpoint and API key, and configure agents via REST API or Azure AI Studio. Then, avoid the OpenAI SDK for Azure-specific agents.

    The below are the steps to achieve it:

    1. Use the correct Azure SDK for Azure AI Agents from Azure AI Services and require the Azure SDK/REST API, not the OpenAI SDK. - https://learn.microsoft.com/en-us/azure/ai-services/agents/quickstart?view=azure-python-preview for Python.
    2. To authenticate with an API key in Azure SDK, use the AzureKeyCredential class along with your endpoint:
         from azure.ai.openai import OpenAIClient
         from azure.core.credentials import AzureKeyCredential
         endpoint = "https://<your-resource-name>.openai.azure.com/"
         api_key = AzureKeyCredential("<your-api-key>")
         client = OpenAIClient(endpoint=endpoint, credential=api_key)
      
    3. If your Agents are configured via Azure AI Studio or REST APIs. There is no direct SDK method like create_agent. Instead use the REST API to create an agent:
         POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.CognitiveServices/accounts/{accountName}/agents?api-version=2024-03-01-preview
           Authorization: Bearer <API_KEY>
         
      
      Include the agent configuration in the request body, you can see an example here in the API documentation - https://learn.microsoft.com/en-us/rest/api/azureai/agents/create
    4. The next is to avoid OpenAI SDK for Azure Agents because the OpenAI SDK (openai library) interacts with OpenAI's platform, not Azure. Use the Azure SDK (azure-ai-openai) for Azure services.

    NOTE: In case there is an issue.

    • Make sure your resource is in a supported region for Azure Agents.
    • Verify your API key has the correct permissions (e.g., Cognitive Services User).
    • Use Azure AI Studio to preconfigure agents visually, then export the code for API integration.

    Regarding additional clarifications:

    Using AzureKeyCredential would always result in an error because the Azure AI Project Client (AIProjectClient) does not support API Key authentication. Instead, it is designed to work with Managed Identities or DefaultAzureCredential() for authentication https://learn.microsoft.com/enus/python/api/overview/azure/aiprojectsreadme?view=azurepythonpreview and https://learn.microsoft.com/enus/azure/aiservices/authentication

    So, try to understand Direct API Key authentication is not possible with AIProjectClient, and use the REST API directly with an API Key for authentication instead of AIProjectClient will be the best option for you. Also, the provided Python below enables API Keybased authentication correctly.

    1. Retrieve Your API Key:
    • Go to Azure Portal > Azure AI Studio > Your AI Project.
      
      Locate the API Key under the "Keys and Endpoints" section.
    1. Use API Key for Direct REST Calls
    Instead of using `AIProjectClient`, make direct API requests with the API Key, this is a code example:
    
       import requests
       api_key = "<yourapikey>"
       endpoint = "https://<yourresourcename>.cognitiveservices.azure.com/"
       headers = {
           "OcpApimSubscriptionKey": api_key,
           "ContentType": "application/json"
       }
       data = {
           "query": "How many feet are in a mile?",
           "parameters": {
               "model": "gpt4o",
               "temperature": 0.7
           }
       }
       response = requests.post(f"{endpoint}/openai/deployments/gpt4o/chat/completions", json=data, headers=headers)
       print(response.json())
    

    You will need to replace <yourapikey> with your actual API key and '<yourresourcename>` with your Azure AI instance name.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions or clarifications.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.