Define and Implement a Basic AI Agent Class

Easy
Agents

What is an AI Agent?

An AI agent is an autonomous system that perceives its environment, makes decisions, and takes actions to achieve a goal.

Task

Implement a basic AIAgent class that:

  1. Accepts a name and system instructions.
  2. Maintains a message history (short-term memory).
  3. Implements a run() method that calls an LLM and returns a response.

Constraints

  • Message history must not exceed 20 messages.
  • Instructions must always be prepended as a system message.
  • Return '[Agent Error]' on any exception.

Examples

Example 1:
Input: agent = AIAgent('Aria', 'You are a helpful assistant.') agent.run('What is 2+2?')
Output: '4'
Explanation: The agent constructs a prompt with the system instruction and user message, calls the LLM, and returns the response.

Starter Code

class AIAgent:
    def __init__(self, name: str, instructions: str):
        # TODO: Initialize agent state
        pass

    def run(self, user_input: str) -> str:
        # TODO: Implement basic agent loop
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews