Managing OpenCode Sessions
Introduction: Working Inside OpenCode
Welcome back! In the previous lesson, you learned how to install OpenCode, configure your API credentials, and launch the application for the first time. However, launching the tool is only the beginning. To be productive, you need to understand the Session Lifecycle: the process of starting a task, managing the conversation, correcting mistakes, and saving your work so you can return to it later.
In this lesson, we will move beyond installation and start doing real work. You will learn how to ask the agent to create and run code, but more importantly, you will learn the essential commands that control the agent itself. We will cover how to fix mistakes with /undo, how to keep your workspace clean with /compact and /new, and how to properly exit and resume your work using /quit and /sessions.
Creating Your First File
The core function of OpenCode is to translate your natural-language requests into actual code and files on your computer. Instead of opening a text editor, creating a new file, saving it, and typing the code yourself, you simply tell the agent what you want. This handles the repetitive work of setting up file structures and syntax, allowing you to focus on the logic of what you want to build.
To see this in action, let's ask the agent to create a simple Python script:
When you press Enter, the agent analyzes your request and executes the necessary file-system operations:
Notice that the agent did two things. First, it decided to name the file hello.py because that is a standard convention for Python scripts. Second, it wrote the correct Python code inside that file. You now have a real file on your hard drive created entirely by the agent.
Running Code Through OpenCode
Once you have a file, the next logical step is to run it. Typically, you would switch to a separate terminal window to execute commands, but Agentic AI can run terminal commands for you. This helps you stay in the flow without constantly switching windows, and it allows the agent to see the output so it can verify its own work.
To run the script you just created, simply ask the agent. Because it remembers the context of your conversation, you don't need to type the full filename:
The agent interprets it as the hello.py file and executes the appropriate command:
The agent identified that it needed to use the python command, executed it, and displayed the output directly in the chat interface. If there had been an error, the agent would have seen it too and could have offered to fix it.
