We are going to build a small CLI agent that reads a Python file and explains its time and space complexity in plain English. It is aimed at self-taught developers who can write loops but still struggle to reason about Big O. Because the agent sends the entire source file as context, Oxlo.ai's flat per-request pricing makes the cost predictable no matter how long the file is.
What you'll need
- Python 3.10 or newer
- The OpenAI SDK:
pip install openai - An Oxlo.ai API key from https://portal.oxlo.ai
Step 1: Craft the system prompt
A tight system prompt keeps the model from drifting into graduate-level analysis. I want it to act like a patient senior engineer sitting next to a junior.
SYSTEM_PROMPT = (
"You are a patient code-complexity tutor for beginner programmers. "
"When given a Python function, do the following:\n"
"1. State the overall time complexity using Big O notation.\n"
"2. State the overall space complexity using Big O notation.\n"
"3. Explain step by step why those complexities apply, referencing specific lines.\n"
"4. If nested loops are present, explain how they multiply.\n"
"5. Keep explanations under 200 words and avoid jargon where possible.\n"
"6. End with one concrete suggestion for improving efficiency."
)
Step 2: Initialize the Oxlo.ai client
The OpenAI SDK is fully compatible with Oxlo.ai, so the only change is the base URL and key. In production I move the key into an environment variable, but for now paste it directly.
from openai import OpenAI
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")
Step 3: Load the target file
I keep this in a small helper so the agent can accept any path from the command line.
def load_code(path: str) -> str:
with open(path, "r", encoding="utf-8") as f:
return f.read()
Step 4: Send the code to the model
Here we package the source into a user message and call Llama 3.3 70B through Oxlo.ai. I picked this model because it handles general reasoning reliably, and with Oxlo.ai's request-based pricing the cost is the same whether the file is ten lines or two hundred. There are also no cold starts, so the CLI feels immediate.
def analyze_complexity(code: str) -> str:
user_message = (
"Analyze the time and space complexity of the following Python code. "
"Teach me like I am a beginner.\n\n"
f"
```python\n{code}\n```
"
)
response = client.chat.completions.create(
model="llama-3.3-70b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
)
return response.choices[0].message.content
Step 5: Add a CLI entrypoint
Tie the pieces together so we can run python complexity_agent.py my_script.py.
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python complexity_agent.py <path_to_python_file>")
sys.exit(1)
source = load_code(sys.argv[1])
report = analyze_complexity(source)
print(report)
Run it
Create a file named bubble_sort.py with a deliberately inefficient implementation.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
Then run the agent:
export OXLO_API_KEY="YOUR_OXLO_API_KEY"
python complexity_agent.py bubble_sort.py
Example output:
Time Complexity: O(n^2)
Space Complexity: O(1)
Explanation:
The outer loop runs n times, where n is the length of the array. For each iteration of the outer loop, the inner loop runs roughly n times as well. Because the loops are nested, we multiply their complexities, giving n * n = n^2. The swap operation inside the inner loop is constant time, O(1).
The space complexity is O(1) because we sort the array in place. We only use a few extra variables for indexing and swapping, and that memory usage does not grow with the input size.
Suggestion:
If you need better performance on large lists, consider using Python's built-in sorted() or list.sort(), which run in O(n log n) time.
Wrap-up
Now that the agent works, you can extend it to accept a GitHub URL or analyze multiple files in a directory. You could also switch the model to DeepSeek V3.2 on Oxlo.ai to experiment with a stronger reasoning model without worrying about token costs on long inputs. For details on request limits and tiers, see https://oxlo.ai/pricing.
Top comments (0)