Katkat

Build an LLM-driven text-game agent

CohereMachine Learning Engineer · Onsite

Last updated

This question tests the ability to design a robust pipeline for an LLM-based agent interacting with a stateful environment, focusing on context management, output validation, and error recovery.

Problem Statement

You are tasked with building the backend logic for an NPC (Non-Player Character) agent in a text-based adventure game. The agent receives a world state description, current inventory, and a history of previous actions, and must output the next action it wants to take.

Your task is to implement the get_agent_action function. The game engine expects a JSON response in the format {"action": "move|use|talk", "target": "string", "reasoning": "string"}.

Requirements

  1. Context Management: The game history can grow very long. Implement a truncation strategy to ensure the total input tokens stay within a limit (e.g., 2000 tokens).
  2. Constrained Parsing: The LLM may occasionally output invalid JSON or hallucinated action types. Implement a robust parsing and retry mechanism.
  3. Fallback Logic: If the model fails to produce a valid action after 3 attempts, the agent must perform a 'pass' action automatically to prevent game hang.

Function Signature

def get_agent_action(current_state, history, llm_client, max_retries=3):
    # current_state: dict containing player location/inventory
    # history: list of strings (previous actions and outcomes)
    # llm_client: mock interface with method `call(prompt)`
    # returns: dict with keys 'action', 'target', 'reasoning'
    pass

Example

Input current_state: {"location": "forest", "inventory": ["rusty_key"]} Input history: ["Player entered forest.", "Player found a locked gate."] Expected Output: {"action": "use", "target": "gate", "reasoning": "I have a rusty key that might fit the gate."}

Hints

Hint 1

Use a sliding window or summarization technique to manage the history list size before injecting it into the prompt.

Hint 2

Wrap your LLM API call in a loop that checks for JSON validity using a library like `json.loads` and verifies the 'action' field against a whitelist.

Hint 3

Consider injecting a 'system prompt' that explicitly defines the JSON schema and provides few-shot examples of correct action formatting.

Follow-up questions

  • How would you measure the performance of this agent? What specific metrics would you track regarding token usage vs. successful action completion?
  • If the game state becomes too large to fit in context, how would you modify the system to provide only relevant state information to the LLM?
  • How would you implement an evaluation suite to ensure that an update to the underlying LLM doesn't degrade the agent's ability to play the game?