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 instanceWorkflows from the Neura Market marketplace related to this DeepSeek resource