Code en technische taken
Op deze pagina
Grok voor ontwikkelaars
xAI biedt een model dat specifiek is geoptimaliseerd voor code: grok-build-0.1 (Grok Build). Dit lichtgewicht model is ontworpen voor pair programming in code-editors. Het is aanzienlijk sneller en goedkoper dan modellen voor algemeen gebruik. Een model zoals Grok 4.5 blijft echter relevant voor complexe debugging en architectuur.
Of u Grok nu gebruikt om code te genereren, te debuggen, te documenteren of te reviewen, de prompt-principes die in deze cursus aan bod komen, zijn van toepassing, met een paar specifieke aandachtspunten.
Codegeneratie
De minimale prompt (vermijd dit)
Create a food tracker
De effectieve prompt
Create a food tracker in Python with Flask.
Features:
- Log a food item with quantity
- Automatic calculation of calories and macronutrients
- Display daily calorie breakdown by nutrient
- High-level overview and trends
Technical constraints:
- SQLite database
- REST API with documented endpoints
- No frontend (API only)
- Unit tests with pytest
- Error handling with appropriate HTTP codes
De documentatie van xAI benadrukt dit punt: specificeer bestandspaden, projectstructuur en afhankelijkheden. Sluit irrelevante informatie uit.
Debugging
Debugging is een use case waarbij context bijzonder belangrijk is. Voorzie het model van alle informatie die nodig is om het probleem te reproduceren.
Gestructureerde debugging-prompt
I have an error in my Node.js/Express application.
Relevant code:
```javascript
app.get('/api/users/:id', async (req, res) => {
const user = await db.query('SELECT * FROM users WHERE id = $1', [req.params.id]);
res.json(user.rows[0]);
});
Error: TypeError: Cannot read properties of undefined (reading ‘rows’)
Context:
- Node.js 20, Express 4.18, pg 8.11
- PostgreSQL 15 locally
- The ‘users’ table exists and contains data
- The error occurs only for certain IDs
My error codes are defined in src/errors.ts. The DB connection file is src/db.ts.
Identify the probable cause and propose a fix.
### Fout met stack trace
Fix this error. Here is the full stack trace:
[paste the stack trace]
Relevant source file: [paste the code]
Configuration:
- Python 3.12, Django 5.0
- PostgreSQL database
- Environment: Docker production
Explain the root cause and propose the fix.
## Documentatie
### Functiedocumentatie
Document this Python function using Google docstring conventions:
def calculate_weighted_score(items, weights=None, normalize=True): if weights is None: weights = [1.0] * len(items) if len(items) != len(weights): raise ValueError(“Mismatch”) total = sum(i * w for i, w in zip(items, weights)) if normalize: total /= sum(weights) return round(total, 4)
Include:
- Function description
- Args with types and descriptions
- Returns with type and description
- Raises with conditions
- A usage example (doctest)
### API-documentatie
Generate OpenAPI (Swagger) documentation for this endpoint:
Endpoint: POST /api/v2/invoices Authentication: Bearer token Body: JSON with client_id (int, required), items (array of objects with description, quantity, unit_price), due_date (ISO 8601, optional) Responses: 201 (created), 400 (validation), 401 (unauthenticated), 500 (server error)
Format: YAML OpenAPI 3.0
## Code review
Review the code in this pull request.
Code: [paste the code]
Evaluation criteria:
- Potential bugs or unhandled edge cases
- Performance (algorithmic complexity, N+1 queries)
- Security (injections, data leaks)
- Readability and maintainability
- Adherence to PEP 8 conventions
For each issue found:
- Line(s) concerned
- Severity: critical / high / low / suggestion
- Explanation of the problem
- Proposed corrected code
## Modelselectie voor code
De documentatie van xAI adviseert:
- **grok-build-0.1**: Snel werk, implementatie, code zoeken, routinematige refactoring
- **Grok 4.5**: Complexe debugging, architectuur, diepgaande conceptuele problemen
Voor code-agents die tool-calling gebruiken, geeft u de voorkeur aan de native tool-calling van grok-build-0.1 boven XML-outputs, omdat dit de prestaties kan verslechteren.
## Cache-optimalisatie
Voor sequentiële interacties (typisch voor pair programming) versnelt de cache van Grok de reacties aanzienlijk. Het voorvoegsel van de conversatie blijft identiek tussen verzoeken, waardoor ophalen uit de cache mogelijk is.
Belangrijke tip: vermijd het wijzigen van de prompt-geschiedenis tussen verzoeken. Elke wijziging verbreekt de cache en vertraagt de inferentie.
## Praktijkoefening
Neem een echte bug uit uw code. Schrijf een gestructureerde debugging-prompt inclusief de code, de fout, de technische context en gerelateerde bestanden. Vergelijk de kwaliteit van het antwoord met een minimale prompt ("fix this bug: [code]").
## Belangrijkste punten
- Specificeer altijd de volledige technische context: taal, framework, versies, projectstructuur
- grok-build-0.1 is geoptimaliseerd voor code: sneller en goedkoper dan modellen voor algemeen gebruik
- Geef voor debugging de code, de volledige foutmelding en de uitvoeringscontext op
- Gebruik native tool-calling in plaats van XML-outputs voor code-agents
- Vermijd het wijzigen van de prompt-geschiedenis om de cache en snelheid te behouden
- Grok 4.5 blijft de beste keuze voor complexe debugging en architectuurbeslissingen