Running an agent from the command line
Agents defined with the OpenAI Agents SDK are meant to run as real programs, not just as isolated function calls. Running an agent from the command line lets us connect it to users, scripts, and operating system workflows in a simple and familiar way. This is how an SDK-defined agent becomes something we can actually use and control.
Invoking an agent from a CLI program
The SDK does not run agents by itself. We still write a small Python program that creates the agent and drives its execution. That program becomes the command-line entry point.
A minimal CLI program usually does three things: create the agent, read input, and print output.
from openai_agents import Agent
agent = Agent(
name="planet_helper",
instructions="Answer questions about planets and moons."
)
This file can be run like any other Python script.
python run_agent.py
At this point, we have an agent instance ready to receive input.
Passing user input to the agent
Command-line programs typically read input from standard input. We pass that input directly to the agent when we invoke it.
while True:
user_input = input("> ")
response = agent.run(user_input)
print(response)
Each line typed into the terminal becomes the agent’s input. The SDK handles reasoning, context, and any internal state while we focus on wiring input to output.
Receiving and displaying agent responses
Agent responses are returned as data from the SDK, not printed automatically. This keeps control in our code.
response = agent.run("Tell me about Europa")
print(response)
Displaying the response is a deliberate step. This makes it easy to format output, log results, or route responses somewhere other than the terminal.
Managing the agent lifecycle
A command-line agent usually runs until we decide to stop it. The simplest approach is to exit the input loop when a specific command is received.
if user_input.strip().lower() == "exit":
break
When the program ends, the agent stops cleanly with it. Any memory or persistence behavior depends on how the agent was configured earlier.
Conclusion
We can now run an SDK-defined agent as a normal command-line program. Input flows from the terminal into the agent, and responses flow back out under our control. This completes the step from defining an agent to actually running one in a real execution environment.