Coding with AI

Motivation and Learning Goals

Motivation
  • Use AI to write Monte Carlo code quickly and accurately. Monte Carlo simulation is tasks with answers, and AI is good at such tasks.
Learning Goals
  • Understand what AI can do when writing code.
  • Understand how to engineer prompts and context.
  • Understand how to validate and review AI outputs.

Disclaimer

  • Things change very quickly. The material in this lecture could become useless next month.
  • This lecture includes some of my personal opinions.

Environment Setup

  • Install Codex or Claude Code.

Which Model Should We Use?

Current Personal Recommendation
  • If you use a popular model such as Codex or Claude, it is already likely to be good enough.
  • I personally recommend Codex because it is more conservative and well suited to our needs.
  • So far, as far as I know, Claude is better at aesthetics, such as web-app and other design work.

Examples of Work Environments

  • VS Code + Codex App
    • I think this is a good starting point.
    • The Codex app is extremely well designed.
  • Codex App only
    • This is good enough, but editing code is slightly more difficult.
  • Cursor only
    • You can use agents in a sidebar.
  • Terminal (Herdr + Neovim) + Codex CLI
  • Choose whichever option best fits your workflow.

Prompt and Context Engineering

  • The general rule is “garbage in, garbage out.”
  • We should control the inputs to AI to obtain the desired outputs.

Prompts

  • Be specific.
  • Effective prompts contain the following elements:
    • Goal
    • Context
    • Constraints
    • Acceptance criteria
Weak Prompt
  • This code is slow. Review it.
Improved Prompt
  • I want to improve the computational speed of the function defined in this file. Diagnose the function and identify the bottleneck. Do not edit any other functions or files.

AGENTS.md (or CLAUDE.md)

  • AI agents are designed to read an AGENTS.md file first.
    • There are two possible locations:
      • global;
      • repository-specific.
  • The file defines how agents should work from the beginning, so it is important.
    • For guidance, see Best practices for ChatGPT.
    • Keep the file concise, at around 100 lines or fewer.
    • Include only durable instructions that should apply across tasks.
    • Put instructions for a single task in the prompt and longer project context in repository documentation.
  • I find the following instructions useful in AGENTS.md:
    • Coding principles
      • I adapted these principles from Andrej Karpathy Skills:
        • Think Before Coding
        • Simplicity First
        • Surgical Changes
        • Goal-Driven Execution
    • Git permissions
      • Allow Git operations only with the user’s permission.
      • This prevents agents from freely using Git, which could damage the repository.
    • Meeting mode
      • Do not let AI write code unless the user allows it to leave the meeting room.

Memory

  • A thread or task can retain context automatically, but managing context becomes difficult as the repository grows.
  • Set up a memory folder in your repository and use AGENTS.md to explain what the folder contains and when agents should read it.
  • Store repository-specific context, such as:
    • the high-level goal of the repository;
    • the order in which tasks should run;
    • general rules for the repository structure.
  • I created a skill that produces a document for this type of memory.
  • A sanitized example based on one of my repositories has the following structure:
    • memory/
      • 00-INDEX.md
        • Routes agents to the relevant memory document.
      • topic-name/
        • YYYY-MM-DD-design-decision.md
        • YYYY-MM-DD-implementation-handoff.md
        • YYYY-MM-DD-workflow-note.md
    • Topic directories group related knowledge.
    • Date prefixes keep documents in chronological order.
  • For a larger memory repository, you should either:
    • use a specific YAML-header structure to prevent AI from reading everything; or
    • introduce a vector database.
  • For reference, see the Claude Code memory documentation.

Skills

  • A “skill” is defined as “a reusable instruction bundle.”
    • One example is a skill that provides paper summaries.
  • I will show some prebuilt skills, but it is more important to create your own skills with AI according to your workflow.

Grill with Docs

  • Grill with Docs asks what you want to accomplish with AI and then builds a concrete plan.
    • I find this skill extremely useful.
    • When I run Monte Carlo experiments, I cannot keep track of every detail.
    • This skill points out what I have left unclear.

Stop Slop

  • Stop Slop removes sloppy AI-generated sentences.

Pedro Sant’Anna’s Skills

Awesome Econ AI Stuff

Validation and Review

  • AI can produce code and outputs extremely quickly.
  • How can we validate and review them?
    • Understanding comes first, of course.
  • Plan what AI will do very carefully.
    • Using Grill with Docs to create a specific plan can help control what AI does.
  • Carefully review outputs, not only code.
    • If you find anything unusual, start by asking AI why it happened.
    • In my experience, when I find unusual graphs or plots, either AI did something wrong or I gave AI an incorrect instruction.
  • Ask questions like a boss or professor.
    • For example:
      • How did you compute this?
      • How did you define this?
      • Do you understand this?
      • Can you explain this?
    • I find many mistakes through this process.
  • Check the code diff.
    • As a final step, check how the code changed.
    • Git is useful for this, but I will discuss it later.

Other Tips and Capabilities

Effort Levels

  • Some LLMs offer multiple effort levels.
    • Current Codex models offer low, medium, high, extra high, max, and ultra.
  • Choosing an appropriate level makes the desired output more likely.

Using LLMs over an SSH Connection

  • With an appropriate setup, you can access the Codex app from another computer or a smartphone.
    • You can write code from your phone.
    • More practically, you can monitor what agents are doing or what code is running from your phone.

Computer Use

  • The Codex app’s computer-use feature is excellent. It can do anything that you can do on your computer.
  • The downside is that you cannot freely use your computer at the same time.

Orchestration

  • We can call subagents to work on tasks concurrently.
  • Orchestration is not essential for ordinary single-task work in this course.
  • It can be useful when tasks are independent, such as separate Monte Carlo experiments.
  • One possible use is to assign experiment A to Agent A and experiment B to Agent B, and then ask them to summarize their results.

Relevant Useful Tools

Voice input tools

  • Typeless
  • Aqua Voice
  • etc…

Coding Terminals

  • Herdr
  • cmux

How to Keep Up with the Latest Discussions

  • Follow economists or software engineers who actively post about these tools on X.
  • I think this is the only way.

Brief Conclusion

  • I have provided general guidance.
  • In later classes, I will demonstrate more specific ways to use these tools.