Rules for building TypeScript/Node.js applications with DeepSeek API, including client patterns, streaming, error handling, and deployment.
## DeepSeek TypeScript/Node.js Development Rules
### Setup
- Use the OpenAI-compatible SDK: `openai` package with custom baseURL
- Configuration:
```typescript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://api.deepseek.com',
apiKey: process.env.DEEPSEEK_API_KEY,
});
```
### Type Safety
- Define interfaces for all prompt inputs and expected outputs
- Use Zod for runtime validation of API responses
- Never use `any` type — create proper types for DeepSeek responses
- Use discriminated unions for different response types (text, code, json)
### Streaming Pattern
```typescript
const stream = await client.chat.completions.create({
model: 'deepseek-chat',
messages: [{ role: 'user', content: prompt }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
```
### Error Handling
- Wrap all API calls in try/catch with typed error handling
- Implement circuit breaker pattern for production
- Use AbortController for request cancellation
- Handle rate limits with exponential backoff and jitter
- Log errors with structured metadata (requestId, model, tokenCount)
### Performance
- Reuse the OpenAI client instance (connection pooling)
- Set appropriate timeouts: 30s for chat, 120s for reasoner
- Use streaming for long responses to improve time-to-first-token
- Cache responses for deterministic prompts (temperature: 0)
### Deployment
- Use environment variables for all configuration
- Health check endpoint that verifies API connectivity
- Graceful shutdown: drain in-flight requests before exit
- Resource limits: set max concurrent requests per instanceSystem rules for designing inter-service communication in microservices architectures with DeepSeek Coder, covering sync/async patterns, error handling, and resilience.
System rules for generating content in multiple languages with DeepSeek V3, covering translation quality, cultural adaptation, locale-specific formatting, and quality assurance.
System rules for safe code refactoring with DeepSeek R1, requiring test coverage verification, incremental changes, and behavior preservation checks.
System rules for using DeepSeek V3 to generate clear, maintainable technical documentation including API docs, architecture docs, and onboarding guides.
System rules for DeepSeek Coder to generate optimized database queries, with requirements for EXPLAIN analysis, indexing recommendations, and performance targets.
System rules for using DeepSeek V3 to generate infrastructure code, CI/CD pipelines, and operational runbooks with security and reliability best practices.