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.

Reverting Mistakes with /undo

Sometimes the agent might misunderstand your instructions, or you might realize you didn't actually want to make that change. In a normal text editor, you would press Ctrl+Z to undo. In OpenCode, you use the /undo command. This safety feature reverts the last action the agent took, whether that was creating a file, editing code, or running a command.

If you ran /undo immediately after creating the file, hello.py would disappear from your file system as if it had never been created. This gives you confidence to experiment, knowing you can always step back.

Note that the /undo command relies on git version control to track and revert changes. For this command to work, your project must be a git repository. If you haven't initialized git yet, you can do so by running git init in your project directory before starting OpenCode. This ensures that all file changes are tracked and can be safely reverted when needed.

Managing Context with /compact

As you work, your conversation history grows. Every message uses tokens, and AI models have limits on how much they can remember at once. Longer histories can also cost more money if you are paying per token. The /compact command helps manage this.

This command summarizes older parts of your conversation, keeping only the essential context needed to continue working:

You won't lose the knowledge of which files exist or what you've been working on, but the agent forgets intermediate steps and small talk, freeing up space for new tasks. Use this command whenever your conversation gets long or the agent starts performing slowly.

Starting Fresh with /new

Sometimes you want to switch tasks completely. Perhaps you finished the Python script and now want to work on a website. If you keep chatting in the same session, the agent might get confused by old context. The /new command starts a fresh conversation without closing the application.

This clears the conversation history so the agent treats your next message as the first one. However, it does not delete your files—only the chat history. Now you have a clean slate without having to exit and restart.

Ending Your Session with /exit

When you are finished working, use /exit to close OpenCode. While you could just close your terminal window, using the proper command ensures your session history is saved to the local database, allowing you to pick up exactly where you left off later.

Once you see your standard terminal prompt, OpenCode has closed. Your files are safe on disk, and the conversation is stored in the application's history.

Note: You can also use /quit as an alias for /exit if you prefer.

Resuming Previous Sessions

Real projects often take days or weeks to complete. OpenCode allows you to resume past sessions so you don't have to re-explain context every time. There are two ways to do this.

If you are already inside OpenCode, use the /sessions command to open an interactive list of past conversations:

Alternatively, if you want to immediately jump back into your most recent work from the terminal, use the --continue flag:

This skips the main interface and immediately loads your last session, perfect for quickly getting back into flow after a break.

Summary and Next Steps

In this lesson, we explored the lifecycle of an OpenCode session. You learned how to direct the agent to create files and run code, moving from a passive user to an active supervisor. We covered the essential commands for managing your workflow:

CommandPurpose
/undoReverts the last action or file change
/compactSummarizes history to save tokens and memory
/newStarts a fresh conversation without exiting
/exitSaves your session and exits the program
/sessionsBrowse and resume past work

These commands form the toolkit you will use every day. In the upcoming practice exercise, you will use these commands to build a small script, correct a mistake, and manage your session history.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal