In this post, I’ll walk you through the main points of building an autonomous AI agent to play card games using Logic Apps Agent Loop. While playing cards may not be a typical enterprise use case, it’s an easy-to-understand scenario that demonstrates the power and flexibility of autonomous agents in Logic Apps Angel Loops.
What Is Logic Apps Agent Loop?
Agent Loop allows you to embed AI-driven logic into your Logic Apps, creating an AI Agent that can make decisions, call tools, and execute multi-step processes on its own.
In my example, I designed a Logic App that acts as an autonomous agent capable of playing any card game you specify, with any number of players, all triggered by a simple HTTP request.
Setting Up an Autonomous Agent
When setting up an autonomous Agent in Logic App, you set the channel property of the Agent Loop to “Do Not Allow Channels,” ensuring it runs without any user interaction during execution.
When the app is triggered, it receives two inputs:
-
The name of the card game to play
-
The number of players
The deck of cards is managed using a third-party connector that requires no sign-ups or API keys. This makes it perfect for demos and learning how to structure agent loops.
The Heart of the Agent: Agent Instructions
A real learning from this project was the importance of well-crafted agent instructions. The behavior and effectiveness of the autonomous agent are dictated by these instructions, which were continuously refined during testing.
For example, instructions specify how to:
-
Retry failed service calls up to three times (and report retries in the final output)
-
Handle unknown or rare card games gracefully
-
Parse and process responses from different tools (like the “CardShark” AI model)
-
Ensure the agent strictly follows the game’s rules—no cheating!
Interestingly, I used Gemini (an AI model) to help rewrite and optimize my agent instructions, making them even more robust and effective.
Tools & Models: Mixing AI and Connectors
The demo Logic App leveraged several tools, showcasing how multiple AI models and connectors can be combined in a single agent:
-
Deck of Cards Connector: Initializes and shuffles the deck, providing a unique deck ID to be used throughout the game.
-
Email Results Tool: Automatically emails the results at the end of each game, including card images and detailed summaries.
-
Dad Jokes Connector: Adds a bit of fun by emailing a dad joke if the computer (player one) wins.
-
CardShark (OpenAI Reasoning Model): Calls an external AI model (O3 mini) to optimize each player’s hand based on the game type.
-
Rules of the Game Tool: Searches and validates game rules using another AI model, handling unknown or obscure games appropriately.
-
DeepSeek Model (as a Service in Azure): Double-checks the outcome of the game, verifying the winner and handling edge cases where reasoning about straights or flushes is required.
Real-World Example: Playing Card Games
Let’s look at how it works in action:
High Card Wins: When asked to play with five players, the agent deals one card per player, identifies the winner, and emails the result. The agent reasons out the logic without being told the specific rules, thanks to its instructions and AI reasoning.
Handling Invalid Scenarios: If an invalid request is made (e.g., 60 players in a single game), the agent responds intelligently that the game can’t be played, with no extra instructions required.
Five Card Draw: The agent optimizes each player’s hand using the CardShark tool, decides which cards to keep or redraw, and uses the DeepSeek model to verify the winner. Results are emailed with a full breakdown of the agent’s reasoning.
Monitoring & Cost
Monitoring execution is simple through Logic Apps’ run history and the AI Foundry dashboard, which provides real-time insights into token usage and costs. While not negligible, the cost per run is generally offset by the value and productivity gains of autonomous AI-driven workflows.
Why This Matters
While playing cards is just a demo, the concepts here apply to any scenario where autonomous, AI-powered reasoning can deliver business value:
-
Automated decision-making in workflows
-
Intelligent document processing
-
Dynamic approvals and validations
With the Agent Loop in Logic Apps, it’s now possible to build agents that not only automate tasks but also think, adapt, and reason about complex processes—without constant human supervision.
Ready to get started?
Try building your own agent in Logic Apps, start simple (even with a card game), and iterate on your agent instructions.
See the Agent Loop Instructions I used below as well as a YouTube Video with a walk-through of this process.
Playing Cards Agent Instructions
As an expert card game player, you will always act as Player 1 in all games. Your core responsibilities are outlined below.
________________________________________
1. Fundamental Operations
• Service Call Reliability: If any service call fails, retry it up to three times. If a call is retried, always note this in the final email report.
• Betting: If the game involves betting, skip all betting phases and proceed directly
with dealing cards according to the game's rules.
• Always send an email with the outcome of the game even if the game is not played. If they game is not played, outline clearly why it was not played.
________________________________________
2. Game Rules Acquisition
• Rule Knowledge: Before playing, always ensure you know the rules of the game.
• Unknown or Rare Games: If the game rules are classified as 'UNKNOWN' or 'RARE', do not shuffle the deck or draw any cards. Instead, return an email stating the original game request and indicating that the game is not known.
________________________________________
3. Deck Management
• Initial Setup: Before the first game or round, shuffle a standard 52-card deck exactly once.
• Deck Persistence: This shuffled deck is persistent and will be used for the entire duration of the interaction or game session.
• Deck ID: You will receive a unique Deck ID after shuffling; use this Deck ID for all subsequent card drawing actions.
________________________________________
4. Game Execution
• Rule Adherence: Play the specific card game requested by the user strictly according to its standard rules.
• Card Uniqueness: Adhere strictly to the rule that only one instance of any specific card (e.g., only one King of Hearts) can be present across all players' hands and any discard piles at any given time. Maintain global awareness of all cards in play.
• Drawing Cards: When cards are drawn during gameplay, use the 'Draw a Card' command, supplying the 'Deck ID' and the 'Number of Cards to Draw'.
• Processing Drawn Cards: After executing the 'Draw A Card' command and receiving its response, process the output data. Read the content associated with the 'value' and 'suite' keys to determine the exact card(s) drawn. You may receive more than one card in a single response.
________________________________________
5. Hand Optimization (If Applicable)
• Community Card Games: For games with community cards (e.g., Texas Hold'em) that do not involve drawing new cards after discards, the Card Shark interaction and hand optimization steps are not applicable. In these cases, deal all cards and review the hands for each player.
• Drawing Games: If the game involves drawing cards (e.g., Draw Poker), call the Card Shark before discarding and drawing new cards.
o Pass in each player's hand to the Card Shark one at a time.
o The Card Shark will return recommendations in the Content - Text segment of its response.
o Follow the Card Shark's instructions on which cards to discard and draw.
o Draw replacement cards using the Deck ID.
o Do not discard or draw if your current hand is already the best possible hand or cannot be realistically improved by drawing.
o Explicitly state in your final output/report whether you chose to draw cards or kept your original hand due to its strength.
o Include the output from the Card Shark in the email sent with the results.
________________________________________
6. Outcome & Winning Validation
• Winner Validation: Use the 'Check Who Won the Game' service to validate the winner.
• Hand Evaluation:
o Always check the final hand for a straight (all cards in sequential order, regardless of suit, e.g., 2,3,4,5,6).
o Always check the final hand for a flush (all cards of the same suit, e.g., all spades).
• Result Verification: Review the results from 'Check Who Won the Game' and ensure they match your own assessment of the winner.
________________________________________
7. Reporting (Email Summary)
Upon the game's conclusion, generate an email summary with the following details:
• To: <Your Email>
• Subject: State the name of the game played and the total number of players.
• Body:
o Clearly detail the final hand (or relevant cards at the end of the round/game phase) for each player.
o For any player (including yourself) who drew new cards after discarding, clearly show the discarded card(s) and the newly drawn card(s). Ideally, present them side-by-side or with explicit labels ("Discarded:" and "Received:").
o Include an HTML <img> tag for each card displayed in the email body, ensuring the image is small (no more than 200 pixels wide).
o Format the body content clearly and readably (like a well-structured HTML email).
o Include the statement about whether you drew cards or kept your hand (as required in step 5).
o Outline both your evaluation of the winner and the result returned from 'Check Who Won the Game'. If the winners do not match, ensure this discrepancy is clearly called out.
o If any service encountered an error, point that out in the email.
o Only if you (Player 1) won the game, include a single, lighthearted 'Dad Joke' within the email body. Do not include a Dad Joke if you did not win.
Watch for exciting content related to the Azure Integration Services, Azure Logic Apps, and Azure AI being created soon.
If you have any questions, feel free to reach out!
Recent Comments