Files · Cohere Code Sandbox for SMB Data Workflows
66 (1 binary, 520.4 kB total)attempt 5
README.md·2538 B·markdown
markdown
# Cohere Code Sandbox for SMB Data Workflows
> Secure, budget-controlled code execution environment for SMB analysts, powered by Cohere’s language models.
This recipe demonstrates how to build a production-grade code sandbox API using Next.js, Cohere’s language models for code classification and safety routing, E2B’s sandboxed code interpreter for secure execution, and REAA Technologies’ agent budget and confidence routing packages for cost control and safety gating.
## Required environment variables
| Variable | Description | How to obtain |
|---|---|---|
| `COHERE_API_KEY` | Cohere API key for language model access | [Cohere Dashboard](https://dashboard.cohere.com/api-keys) |
| `E2B_API_KEY` | E2B API key for sandboxed code execution | [E2B Dashboard](https://e2b.dev/docs/getting-started/api-key) |
## API endpoints
### `POST /api/code`
Execute code in a secure sandbox.
**Request body:**
```json
{
"code": "print('hello world')",
"language": "python",
"maxExecutionTimeMs": 30000
}
```
**Response (200):**
```json
{
"result": "hello world",
"stdout": "hello world",
"stderr": "",
"error": null,
"executionId": "uuid",
"model": "command-a-03-2025",
"cost": 0
}
```
**Error responses:**
- `400` — Invalid request (validation error)
- `403` — Code rejected (safety classification)
- `402` — Budget exceeded
- `500` — Internal server or upstream API error
### `GET /api/code`
Health check with current budget state.
**Response (200):**
```json
{
"status": "ok",
"budget": {
"spent": 0,
"remaining": 5,
"state": "Active",
"limit": 5
}
}
```
## Key packages and their roles
| Package | Role |
|---|---|
| `cohere-ai` | Cohere API client (chat, classification) |
| `@e2b/code-interpreter` | Secure sandboxed code execution |
| `@reaatech/confidence-router` | Code safety routing via confidence scores |
| `@reaatech/confidence-router-classifiers` | Keyword-based code safety classifier |
| `@reaatech/agent-budget-engine` | Budget enforcement and spend tracking |
| `@reaatech/agent-budget-middleware` | Budget interceptor for request lifecycle |
| `@reaatech/llm-router-core` | Model selection with fallback and circuit breaker |
| `zod` | Schema validation for API request/response |
## How to run
```bash
pnpm dev # Start development server
pnpm test # Run tests with coverage
pnpm typecheck # TypeScript type checking
pnpm lint # ESLint static analysis
```
## License
MIT — see [LICENSE](./LICENSE).