OpenCode — Practical Examples & Recipes

Concrete examples showing how to use OpenCode for common developer tasks: scaffolding, refactoring, debugging, tests, CI changes, and security checks.

This page contains short, practical recipes for using OpenCode in everyday developer workflows. Each recipe shows the minimal commands and recommended agent workflow (Plan → Build → Verify).

1) Initialize repository context

When first using OpenCode in a repo, generate the project context so the agent knows file structure and dependencies.

opencode init
# Output: Analyzing project structure... Created AGENTS.md

Workflow notes:

2) Scaffold a new feature (API endpoint + tests)

Goal: add POST /api/widgets with handler, validation, and tests.

Commands / prompts:

  1. Plan: ask the agent to outline the files it will create.

Prompt: “Plan: Add a new POST /api/widgets endpoint. List files, handlers, tests, and migrations (if any).”

  1. Build: allow write operations once plan is approved.

Agent will create files (example):

  1. Verify: run tests locally and ask the agent to fix failures.
# run tests (example)
npm test -- test/widgets.test.ts

3) Large refactor across multiple files

Goal: rename getUserProfilefetchUserProfile and update all imports and docs.

Workflow: