AI agents that take actions autonomously in AI for Everyone - Time & Space Complexity
When AI agents act on their own, it is important to understand how the time they take grows as their tasks or environment get bigger.
We want to know how the number of steps the agent takes changes as the situation becomes more complex.
Analyze the time complexity of the following code snippet.
function autonomousAgent(actions, environment) {
for (let step = 0; step < environment.length; step++) {
let possibleActions = actions.filter(action => action.isValid(environment[step]));
let chosenAction = possibleActions[0];
if (chosenAction) {
chosenAction.execute();
}
}
}
This code lets an AI agent look at each part of its environment, find valid actions, and perform one action per step.
- Primary operation: Looping through each environment step and filtering actions.
- How many times: The loop runs once for each environment element; filtering runs for each step over all actions.
As the environment size grows, the agent checks more steps, and for each step, it checks all actions.
| Input Size (n = environment steps) | Approx. Operations |
|---|---|
| 10 | 10 steps x number of actions checked each step |
| 100 | 100 steps x number of actions checked each step |
| 1000 | 1000 steps x number of actions checked each step |
Pattern observation: The total work grows proportionally with the environment size and the number of actions.
Time Complexity: O(n x m)
This means the time grows in proportion to both the number of environment steps (n) and the number of possible actions (m).
[X] Wrong: "The agent only takes time proportional to the environment size because it acts once per step."
[OK] Correct: At each step, the agent checks all possible actions, so the total time depends on both environment size and number of actions.
Understanding how AI agents scale their work helps you explain how systems handle bigger tasks efficiently, a useful skill in many tech discussions.
"What if the agent only checks a fixed number of actions regardless of environment size? How would the time complexity change?"