Kratos Skills for AI Agents
This skill provides comprehensive go-kratos microservices framework knowledge, optimized for AI agents helping developers build production-ready services.
🎯 When to Use This Skill
Invoke this skill when working with go-kratos:
- Creating services: REST APIs, gRPC services, or microservices architectures
- Layered architecture: Implementing Service → Biz → Data layers with DDD
- Dependency injection: Using Wire for compile-time DI
- Production hardening: Circuit breakers, rate limiting, middleware
- Debugging: Understanding errors, fixing configuration, or resolving issues
- Learning: Understanding kratos patterns and best practices
📚 Knowledge Structure
Load specific guides as needed rather than reading everything at once:
Quick Start
Link: Official Kratos Documentation
Contains: Installation, project creation, basic commands, hello-world examples
Pattern Guides
API & Transport
| File |
When to Load |
| references/api-patterns.md |
Defining Protobuf APIs, generating HTTP/gRPC code |
| references/transport-patterns.md |
HTTP/gRPC server/client configuration |
| references/encoding-patterns.md |
Custom serialization, content negotiation |
| references/openapi-guide.md |
OpenAPI/Swagger documentation generation |
Architecture & Design
| File |
When to Load |
| references/architecture-patterns.md |
DDD layers, repository pattern, Wire DI |
| references/error-patterns.md |
Error definition, assertions, proto errors |
| references/middleware-patterns.md |
Custom middleware, request filtering |
Infrastructure
| File |
When to Load |
| references/config-patterns.md |
Configuration loading, hot reload, config centers |
| references/registry-patterns.md |
Service discovery (etcd, consul, nacos, k8s) |
| references/selector-patterns.md |
Load balancing (P2C, WRR, random) |
Resilience & Reliability
| File |
When to Load |
| references/circuit-breaker-patterns.md |
Fault tolerance, SRE circuit breaker |
| references/ratelimit-patterns.md |
Token bucket, BBR rate limiting |
| references/recovery-patterns.md |
Panic recovery, stack trace logging |
Observability
| File |
When to Load |
| references/logging-patterns.md |
Structured logging, Zap/Logrus adapters |
| references/metrics-patterns.md |
Prometheus metrics collection |
| references/tracing-patterns.md |
OpenTelemetry, Jaeger/Zipkin tracing |
| references/metadata-patterns.md |
Context propagation, trace IDs |
Security & Validation
| File |
When to Load |
| references/auth-patterns.md |
JWT authentication, claims, token generation |
| references/validate-patterns.md |
Proto field validation, protoc-gen-validate |
Data & Tools
| File |
When to Load |
| references/ent-patterns.md |
Ent ORM integration, schema design |
| references/cli-guide.md |
kratos CLI, code generation commands |
Supporting Resources
| File |
When to Load |
| best-practices/overview.md |
Production deployment, code review checklist |
| troubleshooting/common-issues.md |
Debugging errors, protoc/wire issues |
| getting-started/claude-code-guide.md |
Claude Code integration, advanced features |
🚀 Common Workflows
Creating a New Service
- Create project:
kratos new <project-name>
- Define API: Create
.proto with google.api.http annotations
- Generate code:
kratos proto client api/demo/v1/demo.proto
- Generate service:
kratos proto server api/demo/v1/demo.proto -t internal/service
- Implement layers: Biz logic in
internal/biz/, data access in internal/data/
- Configure Wire: Update
cmd/server/wire.go with provider sets
- Run:
go generate ./... && kratos run
Details: references/api-patterns.md
Implementing Layered Architecture
- Define interfaces in
internal/biz/ (biz layer)
- Implement repositories in
internal/data/ (data layer)
- Write use cases in
internal/biz/ (biz layer)
- Implement handlers in
internal/service/ (service layer)
- Create ProviderSets:
data.ProviderSet, biz.ProviderSet, service.ProviderSet
- Wire together in
cmd/server/wire.go
Details: references/architecture-patterns.md
Adding Middleware
http.Middleware(
recovery.Recovery(), // 1. Catch panics first
validate.Validator(), // 2. Validate requests
jwt.Server(keyFunc), // 3. Authentication
ratelimit.Server(limiter), // 4. Rate limiting
logging.Server(logger), // 5. Logging
)
Details: references/middleware-patterns.md
Configuring Service Discovery
// Server-side
reg := etcd.New(client)
app := kratos.New(kratos.Registrar(reg))
// Client-side
dis := etcd.New(client)
conn, _ := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("discovery:///service-name"),
grpc.WithDiscovery(dis),
)
Details: references/registry-patterns.md
⚡ Key Principles
✅ Always Follow
- Layer separation: Service (API) → Biz (Business) → Data (Persistence)
- Dependency Inversion: Interfaces in biz, implementations in data
- Protobuf-first: Define APIs and errors in
.proto files
- Wire injection: Compile-time DI, no global state
- Context propagation: Pass
ctx context.Context through all layers
- Interface-based design: Program to interfaces, not implementations
- Error codes: Structured errors with code, reason, message, metadata
❌ Never Do
- Put business logic in service handlers (violates layered architecture)
- Skip interface definition and use concrete types directly
- Use global variables for dependencies
- Define HTTP handlers manually (use generated code from proto)
- Hard-code configuration values
- Skip validation or forget to check
err != nil
- Modify generated
.pb.go files
📖 Progressive Learning Path
🟢 New to kratos?
- Official Quick Start - Install CLI, create first project
- references/architecture-patterns.md - Understand Service → Biz → Data
- references/api-patterns.md - Learn Protobuf API definition
🟡 Building production services?
- best-practices/overview.md - Production checklist
- references/circuit-breaker-patterns.md + references/ratelimit-patterns.md - Add resilience
- references/registry-patterns.md - Service discovery
- troubleshooting/common-issues.md - Avoid pitfalls
🔵 Extending capabilities?
- getting-started/claude-code-guide.md - Advanced Claude Code features
- Kratos Examples - Example projects
🔗 Kratos Ecosystem
📝 Version Compatibility
- Target version: kratos v2.0.0+
- Go version: Go 1.19 or later recommended
- Protoc: 3.0+
Quick invocation: Use /kratos-skills or ask "How do I [task] with kratos?"
1---2name: kratos-skills3description: Comprehensive knowledge base for go-kratos microservices framework. **Use this skill when:** - Building REST/gRPC APIs with kratos (Service → Biz → Data layered architecture) - Creating microservices with DDD and Clean Architecture patterns - Implementing dependency injection with Wire - Configuring service discovery, load balancing, and resilience patterns - Troubleshooting kratos issues or understanding framework conventions - Generating production-ready microservices code with Protobuf **Features:** - Complete pattern guides with ✅ correct and ❌ incorrect examples - DDD/Clean Architecture enforcement - Production best practices - Common pitfall solutions4license: MIT5---67# Kratos Skills for AI Agents89This skill provides comprehensive go-kratos microservices framework knowledge, optimized for AI agents helping developers build production-ready services.1011## 🎯 When to Use This Skill1213Invoke this skill when working with go-kratos:14- **Creating services**: REST APIs, gRPC services, or microservices architectures15- **Layered architecture**: Implementing Service → Biz → Data layers with DDD16- **Dependency injection**: Using Wire for compile-time DI17- **Production hardening**: Circuit breakers, rate limiting, middleware18- **Debugging**: Understanding errors, fixing configuration, or resolving issues19- **Learning**: Understanding kratos patterns and best practices2021## 📚 Knowledge Structure2223**Load specific guides as needed** rather than reading everything at once:2425### Quick Start26**Link**: [Official Kratos Documentation](https://go-kratos.dev/docs/getting-started/start)27**Contains**: Installation, project creation, basic commands, hello-world examples2829### Pattern Guides3031#### API & Transport32| File | When to Load |33|------|-------------|34| [references/api-patterns.md](references/api-patterns.md) | Defining Protobuf APIs, generating HTTP/gRPC code |35| [references/transport-patterns.md](references/transport-patterns.md) | HTTP/gRPC server/client configuration |36| [references/encoding-patterns.md](references/encoding-patterns.md) | Custom serialization, content negotiation |37| [references/openapi-guide.md](references/openapi-guide.md) | OpenAPI/Swagger documentation generation |3839#### Architecture & Design40| File | When to Load |41|------|-------------|42| [references/architecture-patterns.md](references/architecture-patterns.md) | DDD layers, repository pattern, Wire DI |43| [references/error-patterns.md](references/error-patterns.md) | Error definition, assertions, proto errors |44| [references/middleware-patterns.md](references/middleware-patterns.md) | Custom middleware, request filtering |4546#### Infrastructure47| File | When to Load |48|------|-------------|49| [references/config-patterns.md](references/config-patterns.md) | Configuration loading, hot reload, config centers |50| [references/registry-patterns.md](references/registry-patterns.md) | Service discovery (etcd, consul, nacos, k8s) |51| [references/selector-patterns.md](references/selector-patterns.md) | Load balancing (P2C, WRR, random) |5253#### Resilience & Reliability54| File | When to Load |55|------|-------------|56| [references/circuit-breaker-patterns.md](references/circuit-breaker-patterns.md) | Fault tolerance, SRE circuit breaker |57| [references/ratelimit-patterns.md](references/ratelimit-patterns.md) | Token bucket, BBR rate limiting |58| [references/recovery-patterns.md](references/recovery-patterns.md) | Panic recovery, stack trace logging |5960#### Observability61| File | When to Load |62|------|-------------|63| [references/logging-patterns.md](references/logging-patterns.md) | Structured logging, Zap/Logrus adapters |64| [references/metrics-patterns.md](references/metrics-patterns.md) | Prometheus metrics collection |65| [references/tracing-patterns.md](references/tracing-patterns.md) | OpenTelemetry, Jaeger/Zipkin tracing |66| [references/metadata-patterns.md](references/metadata-patterns.md) | Context propagation, trace IDs |6768#### Security & Validation69| File | When to Load |70|------|-------------|71| [references/auth-patterns.md](references/auth-patterns.md) | JWT authentication, claims, token generation |72| [references/validate-patterns.md](references/validate-patterns.md) | Proto field validation, protoc-gen-validate |7374#### Data & Tools75| File | When to Load |76|------|-------------|77| [references/ent-patterns.md](references/ent-patterns.md) | Ent ORM integration, schema design |78| [references/cli-guide.md](references/cli-guide.md) | kratos CLI, code generation commands |7980### Supporting Resources8182| File | When to Load |83|------|-------------|84| [best-practices/overview.md](best-practices/overview.md) | Production deployment, code review checklist |85| [troubleshooting/common-issues.md](troubleshooting/common-issues.md) | Debugging errors, protoc/wire issues |86| [getting-started/claude-code-guide.md](getting-started/claude-code-guide.md) | Claude Code integration, advanced features |8788## 🚀 Common Workflows8990### Creating a New Service91921. **Create project**: `kratos new <project-name>`932. **Define API**: Create `.proto` with google.api.http annotations943. **Generate code**: `kratos proto client api/demo/v1/demo.proto`954. **Generate service**: `kratos proto server api/demo/v1/demo.proto -t internal/service`965. **Implement layers**: Biz logic in `internal/biz/`, data access in `internal/data/`976. **Configure Wire**: Update `cmd/server/wire.go` with provider sets987. **Run**: `go generate ./... && kratos run`99100**Details**: [references/api-patterns.md](references/api-patterns.md)101102### Implementing Layered Architecture1031041. **Define interfaces** in `internal/biz/` (biz layer)1052. **Implement repositories** in `internal/data/` (data layer)1063. **Write use cases** in `internal/biz/` (biz layer)1074. **Implement handlers** in `internal/service/` (service layer)1085. **Create ProviderSets**: `data.ProviderSet`, `biz.ProviderSet`, `service.ProviderSet`1096. **Wire together** in `cmd/server/wire.go`110111**Details**: [references/architecture-patterns.md](references/architecture-patterns.md)112113### Adding Middleware114115```go116http.Middleware(117 recovery.Recovery(), // 1. Catch panics first118 validate.Validator(), // 2. Validate requests119 jwt.Server(keyFunc), // 3. Authentication120 ratelimit.Server(limiter), // 4. Rate limiting121 logging.Server(logger), // 5. Logging122)123```124125**Details**: [references/middleware-patterns.md](references/middleware-patterns.md)126127### Configuring Service Discovery128129```go130// Server-side131reg := etcd.New(client)132app := kratos.New(kratos.Registrar(reg))133134// Client-side135dis := etcd.New(client)136conn, _ := grpc.DialInsecure(137 context.Background(),138 grpc.WithEndpoint("discovery:///service-name"),139 grpc.WithDiscovery(dis),140)141```142143**Details**: [references/registry-patterns.md](references/registry-patterns.md)144145## ⚡ Key Principles146147### ✅ Always Follow148149- **Layer separation**: Service (API) → Biz (Business) → Data (Persistence)150- **Dependency Inversion**: Interfaces in biz, implementations in data151- **Protobuf-first**: Define APIs and errors in `.proto` files152- **Wire injection**: Compile-time DI, no global state153- **Context propagation**: Pass `ctx context.Context` through all layers154- **Interface-based design**: Program to interfaces, not implementations155- **Error codes**: Structured errors with code, reason, message, metadata156157### ❌ Never Do158159- Put business logic in service handlers (violates layered architecture)160- Skip interface definition and use concrete types directly161- Use global variables for dependencies162- Define HTTP handlers manually (use generated code from proto)163- Hard-code configuration values164- Skip validation or forget to check `err != nil`165- Modify generated `.pb.go` files166167## 📖 Progressive Learning Path168169### 🟢 New to kratos?1701. [Official Quick Start](https://go-kratos.dev/docs/getting-started/start) - Install CLI, create first project1712. [references/architecture-patterns.md](references/architecture-patterns.md) - Understand Service → Biz → Data1723. [references/api-patterns.md](references/api-patterns.md) - Learn Protobuf API definition173174### 🟡 Building production services?1751. [best-practices/overview.md](best-practices/overview.md) - Production checklist1762. [references/circuit-breaker-patterns.md](references/circuit-breaker-patterns.md) + [references/ratelimit-patterns.md](references/ratelimit-patterns.md) - Add resilience1773. [references/registry-patterns.md](references/registry-patterns.md) - Service discovery1784. [troubleshooting/common-issues.md](troubleshooting/common-issues.md) - Avoid pitfalls179180### 🔵 Extending capabilities?1811. [getting-started/claude-code-guide.md](getting-started/claude-code-guide.md) - Advanced Claude Code features1822. [Kratos Examples](https://github.com/go-kratos/examples) - Example projects183184## 🔗 Kratos Ecosystem185186| Project | Purpose |187|---------|---------|188| [kratos](https://github.com/go-kratos/kratos) | Framework core, CLI tools |189| [kratos-layout](https://github.com/go-kratos/kratos-layout) | Official project template |190| [contrib](https://github.com/go-kratos/kratos/tree/main/contrib) | Plugins for config, registry, log, metrics |191| [aegis](https://github.com/go-kratos/aegis) | Availability algorithms |192| [gateway](https://github.com/go-kratos/gateway) | API Gateway |193| [examples](https://github.com/go-kratos/examples) | Example code |194195## 📝 Version Compatibility196197- **Target version**: kratos v2.0.0+198- **Go version**: Go 1.19 or later recommended199- **Protoc**: 3.0+200201---202203**Quick invocation**: Use `/kratos-skills` or ask "How do I [task] with kratos?"