Observation-Thought-Action Cycle Implementation

Easy
Agents

Observation-Thought-Action Cycle

The OTA cycle is the atomic unit of agent execution.

Task

Implement OTACycle that:

  1. Takes an observation and runs it through think and act functions.
  2. Records each step as a Step dataclass.
  3. Returns formatted context of the last N steps for the next iteration.

Constraints

  • think_fn(observation: str) -> str
  • act_fn(thought: str) -> str
  • Context format: Obs: ... | Thought: ... | Action: ... per step.

Examples

Example 1:
Input: cycle = OTACycle() step = cycle.process('User asked for 2+2', lambda o: 'I should add', lambda t: 'call_add(2,2)') step.thought
Output: 'I should add'
Explanation: Think function transforms observation into thought.

Starter Code

from dataclasses import dataclass, field
from typing import List, Optional

@dataclass
class Step:
    observation: str
    thought: str
    action: str
    result: Optional[str] = None

class OTACycle:
    def __init__(self):
        self.steps: List[Step] = []

    def process(self, observation: str, think_fn, act_fn) -> Step:
        # TODO: Run one OTA cycle and record step
        pass

    def get_context(self, last_n: int = 3) -> str:
        # TODO: Return formatted string of last N steps
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews