10 Coding with AI tools
This appendix is a compact quick reference. For the full treatment (LLM basics, agentic workflows, API keys, and reproducibility), see AI Tooling for Research: LLMs and Agentic Workflows.
10.1 Quick Workflow
- Define the task in one sentence.
- Add context (data structure, constraints, goal).
- Ask for a small, testable output first.
- Run and verify before asking for extensions.
- Log what AI produced and what you changed.
10.2 Prompt Template
Task:
Context:
Constraints:
Output format:
Validation checks:
For an agent that can use tools, also specify which files it may read, which actions it may take, what result to return, and when to stop and ask you. For example: “Inspect the campaign join; read the two preparation scripts and run diagnostics, but propose edits before changing files. Report the row counts before and after the join.”
10.3 Chat or Agent?
- Chat: explain an error message, compare methods, or suggest a short R snippet.
- Agent: inspect several files, run a diagnostic, and revise code based on what it finds.
- Existing script or Make rule: repeat a well-defined, already-tested process. No agent needs to rediscover the steps each time.
10.4 High-Value Uses
- Explain error messages in plain language.
- Draft boilerplate R code for repeated tasks.
- Refactor long scripts into smaller functions.
- Suggest edge-case tests for data cleaning.
- Improve readability of comments and docs.
10.5 Red Flags
- References that cannot be verified.
- Code that runs but uses the wrong variables.
- Methods suggested without assumptions.
- Confident answers with no uncertainty.
10.6 Minimum Verification Checklist
- Re-run code from a clean session.
- Check joins, keys, and date logic.
- Confirm citations exist and are relevant.
- Compare outputs to a baseline sanity check.
- Document where AI influenced decisions.
10.7 Safety Reminders
- Never paste API keys or sensitive data into prompts.
- Keep credentials in environment variables.
- Check whether project data may be sent to the AI provider; a private repository alone does not settle that question.
- Revoke and rotate exposed keys immediately.