diff --git a/python/samples/getting_started/agents/foundry/foundry_with_existing_agent.py b/python/samples/getting_started/agents/foundry/foundry_with_existing_agent.py index 1b525bdd26..5c02b25e1e 100644 --- a/python/samples/getting_started/agents/foundry/foundry_with_existing_agent.py +++ b/python/samples/getting_started/agents/foundry/foundry_with_existing_agent.py @@ -24,26 +24,27 @@ async def main() -> None: print("=== Foundry Chat Client with Existing Agent ===") # Create the client - client = AIProjectClient(endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"], credential=AzureCliCredential()) + async with AIProjectClient( + endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"], credential=AzureCliCredential() + ) as client: + # Create an agent that will persist + created_agent = await client.agents.create_agent( + model=os.environ["FOUNDRY_MODEL_DEPLOYMENT_NAME"], name="WeatherAgent" + ) - # Create an agent that will persist - created_agent = await client.agents.create_agent( - model=os.environ["FOUNDRY_MODEL_DEPLOYMENT_NAME"], name="WeatherAgent" - ) - - try: - async with ChatClientAgent( - # passing in the client is optional here, so if you take the agent_id from the portal - # you can use it directly without the two lines above. - chat_client=FoundryChatClient(client=client, agent_id=created_agent.id), - instructions="You are a helpful weather agent.", - tools=get_weather, - ) as agent: - result = await agent.run("What's the weather like in Tokyo?") - print(f"Result: {result}\n") - finally: - # Clean up the agent manually - await client.agents.delete_agent(created_agent.id) + try: + async with ChatClientAgent( + # passing in the client is optional here, so if you take the agent_id from the portal + # you can use it directly without the two lines above. + chat_client=FoundryChatClient(client=client, agent_id=created_agent.id), + instructions="You are a helpful weather agent.", + tools=get_weather, + ) as agent: + result = await agent.run("What's the weather like in Tokyo?") + print(f"Result: {result}\n") + finally: + # Clean up the agent manually + await client.agents.delete_agent(created_agent.id) if __name__ == "__main__":