Building an Agent Client App
You've created an agent in the portal β now connect it to your own application. Learn how to build a lightweight Python client that interacts with your Foundry agent.
From portal to code
Building an agent client app is like putting a phone line into your agentβs office.
In the last module, you created an agent in the Foundry portal and tested it there. Now youβre building a Python app that connects to that same agent β so your users can interact with it from your own application, website, or service.
The portal is great for testing. But real users need your agent embedded in YOUR product.
Chat app vs agent app
| Feature | Chat App (Module 14) | Agent App (This Module) |
|---|---|---|
| API used | Chat Completions API | Responses API (Agents) |
| Can use tools? | No β text only | Yes β function calling, search, code |
| State management | You manage conversation history manually | Foundry manages conversations and state |
| Use case | Simple Q&A, summarisation, translation | Multi-step tasks, tool use, autonomous actions |
Building an agent client: step by step
Step 1: Connect and get your agent
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
client = AIProjectClient(
credential=DefaultAzureCredential(),
endpoint="https://your-project.services.ai.azure.com"
)
# Get the agents client
agents = client.agents
Step 2: Create a conversation and send a message
# Create a new conversation (thread)
response = agents.create_response(
agent_id="your-agent-id",
input="What's the refund policy for orders over $500?",
)
# Display the agent's response
print(response.output_text)
Whatβs happening:
agent_ididentifies which agent to talk to (found in the Foundry portal)inputis the userβs message- The agent automatically decides which tools to use
output_textcontains the final response after all tool calls complete
Step 3: Continue the conversation
# Send a follow-up message in the same conversation
response = agents.create_response(
agent_id="your-agent-id",
input="Can I get a refund for my order #12345?",
previous_response_id=response.id # Links to the previous conversation
)
print(response.output_text)
Key difference from chat apps: You donβt need to send the full conversation history. The previous_response_id tells Foundry to continue the same conversation β the agent remembers the context.
What happens behind the scenes
When you send a message to an agent, hereβs what happens:
- User message received β your app sends the input
- Agent reasons β the LLM reads the input, instructions, and available tools
- Tool selection β if needed, the agent decides which tool to call
- Tool execution β Foundry runs the tool (search, code, API call)
- Tool result β the tool returns data to the agent
- Response generation β the agent generates a final response using the tool results
- Response returned β your app receives the complete response
Steps 3-5 may repeat multiple times if the agent needs multiple tools.
Handling tool outputs in your app
Sometimes you want to see what tools the agent used:
response = agents.create_response(
agent_id="your-agent-id",
input="Look up order #12345 and tell me its status",
)
# Check what the agent did
for item in response.output:
if item.type == "message":
print(f"Agent said: {item.content[0].text}")
elif item.type == "function_call":
print(f"Agent called: {item.name} with {item.arguments}")
GreenLeaf scenario: GreenLeaf builds a client app where farmers can ask questions about their orders. The agent:
- Receives: βWhereβs my seed order?β
- Calls:
lookup_orderfunction with the farmerβs ID - Calls:
check_shippingfunction with the order number - Responds: βYour seed order #789 shipped yesterday via courier. Expected delivery: Thursday.β
π¬ Video walkthrough
π¬ Video coming soon
Building an Agent Client App β AI-901 Module 16
Building an Agent Client App β AI-901 Module 16
~14 minFlashcards
Knowledge Check
Priya is building an agent client app. She notices that unlike her chat app (Module 14), she doesn't need to send the full conversation history with each message. Why?
GreenLeaf's agent client app receives the response after the agent looked up an order and checked shipping. Which part of the response object contains the agent's final answer to the user?
Next up: Building a Text Analysis App β using Azure AI Language to extract insights from text.