# Groq Fortran coding agent

**URL:** <https://fortran-lang.discourse.group/t/groq-fortran-coding-agent/9315>\
**Category:** AI\
**Created:** [March 8, 2025, 4:04pm UTC](https://fortran-lang.discourse.group/t/groq-fortran-coding-agent/9315 "2025-03-08T16:04:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 8, 2025, 4:04pm UTC](https://fortran-lang.discourse.group/t/groq-fortran-coding-agent/9315/1 "2025-03-08T16:04:01Z")

</div>

The Groq API (Groq is distinct from grok) gives you access to multiple LLMs, run on the fast GroqCloud. You need an API key., but there is a free tier that allows for considerable usage. I created a Python agent that given a coding prompt, automates the process of sending gfortran error messages back to the LLM until the code compiles. For a configuration file

```auto
model: qwen-2.5-coder-32b
max_attempts: 10
max_time: 1000
prompt_file: prompt_cauchy.txt
source_file: cauchy.f90
run_executable: yes
print_code: no
print_compiler_error_messages: no
compiler: gfortran
compiler_options: -O0 -Wall -Werror=unused-parameter -Werror=unused-variable -Werror=unused-function -Wno-maybe-uninitialized -Wno-surprising -fbounds-check -static -g

```

with the file `prompt_cauchy.txt` containing

> Do a Fortran simulation to find the optimal trimmed mean estimator of  
> the location of the Cauchy distribution, trying trimming proportions  
> of 0%, 10%, 20%, 30%, 40%, and 45%. Declare real variables as  
> real(kind=dp) with dp a module constant, and put procedures in a  
> module. Have the simulation use 100 samples of 1000 observations each.  
> Only output Fortran code. Do not give commentary.

sample output was

```auto
Attempt 1 failed (error details suppressed, generation time: 3.164 seconds, LOC=67)
Attempt 2 failed (error details suppressed, generation time: 14.672 seconds, LOC=71)
Code compiled successfully after 3 attempts (generation time: 18.061 seconds, LOC=75)!
Running executable: .\cauchy.exe

Output:
  Trim proportion: 0.0000000000000000 Mean trimmed mean: -2.7425188462552290
 Trim proportion: 0.10000000000000001 Mean trimmed mean: 1.6141409422076695E-002
 Trim proportion: 0.20000000000000001 Mean trimmed mean: 1.4961654913832745E-002
 Trim proportion: 0.29999999999999999 Mean trimmed mean: 1.2341060294325419E-002
 Trim proportion: 0.40000000000000002 Mean trimmed mean: 1.1169627560190555E-002
 Trim proportion: 0.45000000000000001 Mean trimmed mean: 1.0791625569573506E-002

Total generation time: 35.897 seconds across 3 attempts

Compilation command: gfortran -O0 -Wall -Werror=unused-parameter -Werror=unused-variable -Werror=unused-function -Wno-maybe-uninitialized -Wno-surprising -fbounds-check -static -g -o cauchy cauchy.f90

```

The available models are listed [here](https://console.groq.com/docs/models). The project, which has a single Python source file, is at

> **[GitHub - Beliavsky/Groq-Fortran-agent: Python script that uses Groq to create Fortran...](https://github.com/Beliavsky/Groq-Fortran-agent)**
>
> Python script that uses Groq to create Fortran programs, iterating until they compile

Running the same script many times with the same model, the number of attempts needed for a program that compiles varies a lot. Sometimes the executable gives wrong results or has a run-time error. The script could modified so that specified object or library files are compiled along with the generated source file to create an executable.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 8, 2025, 10:05pm UTC](https://fortran-lang.discourse.group/t/groq-fortran-coding-agent/9315/2 "2025-03-08T22:05:20Z")

</div>

I uploaded to GitHub analogous Python scripts to generate [C++](https://github.com/Beliavsky/Groq-cpp-agent) and [Python](https://github.com/Beliavsky/Groq-Python-agent) code. For the problem of estimating the mode of Cauchy variates using the trimmed mean, the C++ agent is often able to do it on the first attempt, because C++ has built-in functions to generate Cauchy variates and to sort arrays. The Python agent was told to use NumPy and pandas – it chose to use SciPy also.
