โก Ktor
๐ Table of Contentsโ
- โก Ktor
This framework combines 5W1H with Good Prompt principles (Clear role ยท Clear format ยท Clear goal ยท Clear context ยท Clear examples) and clearly separates context-owned vs user-owned responsibilities.
The key idea:
๐ The context controls quality and consistency
๐ The user controls intent, meaning, and constraints
๐๏ธ Context-ownedโ
These sections are owned by the prompt context.
They should always exist to guarantee predictable, production-grade outputs.
๐ค Who (Role / Persona)โ
Who should the AI act as?
Default Persona (Recommended)โ
- You are a senior backend engineer specializing in Kotlin + Ktor
- Think like a technical lead / backend architect
- Assume production usage by default
- Balance idiomatic Kotlin with pragmatic system design
Expected Expertiseโ
- Kotlin (coroutines, suspend functions)
- Ktor (routing, plugins, pipelines)
- RESTful API design
- Async & non-blocking I/O
- Serialization (kotlinx.serialization / Jackson)
- Authentication (JWT, OAuth2 basics)
- Database access (Exposed / jOOQ / JDBC)
- Performance, scalability, and maintainability
โ Sets engineering depth, bias, and trade-offs
โ ๏ธ Should always be present (ideally via .cursor/rules.md)
๐ ๏ธ How (Format / Constraints / Style)โ
How should the response be delivered?
๐ฆ Format / Outputโ
- Use Kotlin + Ktor code snippets when applicable
- Separate layers clearly:
routesservicesrepositoriesdomain / models
- Use:
- Code blocks for all code
- Bullet points for explanations
- Tables for trade-offs when relevant
โ๏ธ Constraints (Ktor Best Practices)โ
- Kotlin 1.9+
- Ktor 2.x
- Coroutine-first design (
suspend, structured concurrency) - Non-blocking I/O by default
- Explicit plugin configuration
- Avoid blocking calls in coroutines
- Avoid business logic in routes
- Avoid framework leakage into domain logic
- Prefer configuration via
application.conf/application.yaml - Externalize secrets (never hardcode credentials)
๐งฑ Architecture & Design Rulesโ
- RESTful resource naming (
/users,/orders/{id}) - Version APIs explicitly (
/api/v1/...) - Proper HTTP status codes (
201,204,400,404,409) - DTOs for API boundaries
- Explicit request/response models
- Centralized error handling (StatusPages)
- Clear separation of concerns
- Stateless services where possible
- Business logic lives in services
- Repositories handle persistence only
- Prefer composition over inheritance
๐ Security & Validationโ
- Validate input explicitly
- Fail fast on invalid requests
- Never trust client input
- Do not leak internal errors or stack traces
- Map domain errors to meaningful HTTP responses
- Keep security-sensitive logic in services
- Be explicit about authentication and authorization boundaries
๐งช Reliability & Maintainabilityโ
- Small, focused suspend functions
- Explicit coroutine scopes and dispatchers
- Avoid hidden blocking calls
- Model failures explicitly (sealed classes / Result)
- Prefer immutability
- Clear method naming
- Log at system boundaries
- Explain why when trade-offs exist
- Prefer clarity over clever DSL abuse
๐ Explanation Styleโ
- Concise and practical
- Explain decisions briefly after code
- Avoid unnecessary theory unless requested
โ
Controls code quality, consistency, and usability
๐ Ideal for .cursor/rules.md
โ๏ธ User-ownedโ
These sections must come from the user.
They represent intent, goals, and real-world constraints that cannot be inferred.
๐ What (Task / Action)โ
What do you want the AI to do?
Examples:
- Implement a REST API endpoint in Ktor
- Review Ktor routing or coroutine usage
- Debug performance or concurrency issues
- Design a Ktor-based backend
- Compare Ktor libraries or patterns
โ
Defines the core engineering task
๐ Always required
๐ฏ Why (Intent / Goal)โ
Why are you asking? Whatโs the desired outcome?
Examples:
- Improve maintainability
- Ensure coroutine best practices
- Support an architectural decision
- Standardize backend patterns
โ Guides depth, trade-offs, and prioritization
๐ Where (Context / Situation)โ
In what technical context does this apply?
Examples:
- Ktor microservice vs monolith
- PostgreSQL / MySQL
- Kubernetes / VM / local
- Greenfield vs migration from Spring
โ ๏ธ Optional, but highly impactful
โฐ When (Time / Phase / Lifecycle)โ
When is this being used?
Examples:
- MVP
- Production hotfix
- Refactor phase
- Early design exploration
โ ๏ธ Optional, but helps tune rigor and risk
๐ Final Prompt Template (Recommended Order)โ
1๏ธโฃ Persistent Context (Put in .cursor/rules.md)โ
# Backend Engineering AI Rules โ Ktor
You are a senior backend engineer specializing in Kotlin + Ktor.
Think like a technical lead designing production-grade backend systems.
## Technology
- Kotlin 1.9+
- Ktor 2.x
- Coroutines, non-blocking I/O
## Core Principles
- Assume production usage by default
- Prefer clarity, correctness, and maintainability
- Avoid unnecessary abstractions
## Concurrency
- Coroutine-first design
- Avoid blocking calls
- Use explicit dispatchers
## API Design
- RESTful resource naming
- Version APIs (`/api/v1/...`)
- Proper HTTP status codes
- Use DTOs for API boundaries
## Architecture
- Routes: request/response mapping only
- Services: business logic
- Repositories: data access only
- Centralized error handling (StatusPages)
## Validation & Security
- Validate all external input
- Never trust client data
- Do not leak internal errors
- Keep security-sensitive logic in services
## Code Style
- Meaningful names
- Small, focused suspend functions
- Prefer immutability
## Configuration
- Use `application.conf` / `application.yaml`
- Use environment-based overrides
- Never hardcode secrets
2๏ธโฃ User Prompt Template (Paste into Cursor Chat)โ
Task:
[Describe exactly what you want to build, review, debug, or design.]
Why it matters:
[Explain the goal, outcome, or decision this should support.]
Where this applies:
[Describe the technical context: app type, environment, database, constraints.]
(Optional)
When this is needed:
[Project phase, urgency, or lifecycle stage.]
(Optional)
โ Fully Filled Exampleโ
Task:
Implement a REST API for managing users using Ktor.
Why it matters:
This service will serve as a Kotlin-first reference and should demonstrate idiomatic coroutine usage and clean architecture.
Where this applies:
In a Ktor microservice deployed on Kubernetes, using PostgreSQL.
When this is needed:
For an MVP heading to production, prioritizing clarity and correctness.
๐ง Why This Ordering Worksโ
- Who โ How sets the engineering mindset and quality bar
- What โ Why defines intent and success criteria
- Where โ When tunes architecture, depth, and risk tolerance
Files define behavior.
Prompts define intent.
Context makes the answer production-ready.
Happy Ktor Prompting โก๐