Course contentsShow
← AI Engineering
Lesson 632 of 1,88616. AI Agents - ImplementationFree lesson

Action Selection and Parsing

Parse LLM output to extract structured action commands and parameters.

Action Selection and Parsing

You'll learn: How to extract structured action commands and their parameters from an LLM's free-form text output so your agent can execute them reliably.

The Problem with Free-Form Output

When your Decision Module (lesson 631) decides what to do next, the LLM typically generates text like: "I should search for weather data using the location 'Paris'." But your agent needs structured data like {"action": "search_weather", "location": "Paris"} to actually execute the tool.

Parsing Strategies

Structured Output Prompting: Instruct the LLM to return responses in a specific format like JSON or XML. Example prompt snippet: "Return your decision as JSON with 'action' and 'parameters' fields."

Regex Pattern Matching: Use regular expressions to extract action names and arguments from predictable text patterns. Works well when output format is consistent.

Function Calling APIs: Let the LLM return pre-structured function calls directly (as covered in lessons 543-584). This offloads parsing complexity to the model provider.

Fallback Parsing: If the primary format fails, try alternative patterns or ask the LLM to reformat its response before failing completely.

Action Selection Logic

Once parsed, your agent validates:

  • Is this action available in the current context? (lesson 581)
  • Do parameters match expected types and constraints? (lesson 577)
  • Does this action make sense given the current state? (lesson 619)

Think of it like translating speech into button presses on a control panel—you need to hear "turn left," understand it, then map it to the exact left-turn mechanism.

Key Takeaway: Action selection bridges the gap between LLM reasoning and executable commands by parsing free-form output into validated, structured actions your agent can reliably perform.