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:
- Mode:
Planfirst to review whatopencode initwill analyze. - Permissions:
askfor file writes the first run.
2) Scaffold a new feature (API endpoint + tests)
Goal: add POST /api/widgets with handler, validation, and tests.
Commands / prompts:
- 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).”
- Build: allow write operations once plan is approved.
Agent will create files (example):
src/pages/api/widgets.tssrc/lib/widgets.service.tstest/widgets.test.ts
- 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 getUserProfile → fetchUserProfile and update all imports and docs.
Workflow:
- Use Plan mode to produce a safe refactor plan and list of files.
- Use
@explore find getUserProfileif the agent misses files.