Context
Carbon-Sync, codenamed Project Jumanji, was built between February and April 2025. The goal was to give small and commercial farms a carbon number from imagery they can actually get, without a field survey. I wrote 74 of the repository’s 78 commits. Two collaborators merged early pull requests.
The repository holds three parts. hack/ is the carbon credit graph. mars/ is an agronomy advisor graph that reuses the same image pipeline and adds pest detection and weather. app/ runs the image pipeline as batch jobs over many image pairs on a distributed compute network. Both graphs are registered in langgraph.json and build into a Docker image with the LangGraph CLI.
The problem
Carbon markets pay per tonne of CO2 equivalent, but the measurement step is expensive. A farm needs an estimate it can recompute from its own images, with every step visible. The model must not invent the physics, and the system must refuse images that are not farmland before it computes anything.
Design
Image pipeline (compute.py). Input is a five-band multispectral TIFF read from S3. The module calls it satellite imagery, but the advisor prompt calls it drone-captured and the credit step assumes 0.01 m² pixels, so in practice this is low-altitude aerial imagery.
- Radiometric correction. Digital numbers become radiance through per-band gain and offset. Radiance becomes top-of-atmosphere reflectance from the sun elevation, Earth to Sun distance and per-band solar irradiance. A dark-object subtraction then removes haze: the mean of each band’s darkest 1% of pixels, adjusted by 1%, is subtracted and the result clipped to 0 to 1.
- Noise reduction. A 3 by 3 median filter per band, with an optional thread-pool version that filters bands in parallel.
- NDVI. (NIR minus Red) over (NIR plus Red), with red as band 2 and NIR as band 4. It also reports the RMSE between NDVI before and after filtering.
- Alignment. The batch version in
app/aligns each target image to a reference with ORB keypoints and a RANSAC homography before computing NDVI.
Carbon graph (hack/graph.py). Two nodes and a conditional edge:
analyzesends a ground-level JPEG togpt-4o-miniat temperature 0. It must return a PydanticPlantAnalysisOutput(plant, status, carbon fraction, root to shoot ratio), picking the crop from a fixed table in the prompt: maize, soybean, wheat or rice. A non-plant image returns “deny” and the graph ends.aerialruns a tool-calling agent. Its one tool computes NDVI from the multispectral image, and the agent approves or denies the image as real farmland. On approval the node runs the credit calculation.
Credit calculation. Above-ground biomass is 2.5 × e^(1.2 × NDVI) kg/m² per pixel. Root biomass is above-ground biomass times the crop’s root to shoot ratio, with 0.5 as the fallback. Carbon is total biomass times the carbon fraction, with 0.47 as the fallback. CO2 is carbon times 44/12. Credits are the sum over pixels times pixel area, divided by 1,000, so one credit is one tonne of CO2 equivalent. The repo computes credit volume. It does not attach a market price.
Advisor graph (mars/recommendation.py). A weather node pulls seven past days and a three-day forecast from Open-Meteo, including FAO reference evapotranspiration (ET0). A router sends TIFFs through the NDVI node and everything else to pest detection. The NDVI node turns NDVI into a crop coefficient (1.25 × NDVI + 0.1, clipped to 0.1 to 1.2) and estimates crop water requirement as mean coefficient times today’s ET0. A YOLO11s model fine-tuned on the 102-class IP102 pest dataset finds pests. A final node asks Claude 3 Haiku for a JSON risk level and plain-language advice.
Guardrails
- Two refusal gates. The carbon graph stops at the first node if the ground photo is not a plant, and raises “Image rejected!” if the NDVI check says the aerial image is not farmland. No credits are computed for a rejected image.
- Parameters from a table, not from the model’s memory. The model may only choose a crop from a four-row table. Its carbon fraction and root to shoot ratio come from that table, and code applies documented defaults if parsing fails.
- Arithmetic outside the model. The credit calculation is plain NumPy. The LLM never sees biomass or credit values.
- Typed outputs. If the advisor’s JSON cannot be parsed, it returns “Unknown” with the parse error instead of crashing.
- Balanced training data. Before fine-tuning,
data_balancer.pysampled or oversampled every IP102 class to 500 instances, so common pests did not drown out rare ones.
Evals and results
- Pipeline timing. After I replaced rasterio with tifffile for reading TIFFs, the recorded notebook run of the whole pipeline on one test image pair went from 9.38 to 7.72 seconds.
- Pest model. The model card reports mAP@0.5 of 0.941, mAP@0.5:0.95 of 0.838, precision 0.923 and recall 0.907. The training logs are not in the repo, so I cannot reproduce those numbers from it.
- Smoke scripts. Two scripts run the plant check on a sample maize photo and the aerial check on a test TIFF. They assert nothing.
What is not measured: the biomass coefficients (2.5 and 1.2) and the crop table were never calibrated against field biomass samples. No credit figure was checked against a registry methodology. The calculation also prices standing carbon stock in a single image, not change over time against a baseline, which is what a real credit needs. There are no unit tests, and the last commit notes that cloud deployment was still to do.
A failure I handled
The advisor graph worked locally and broke as a deployable service. Each node wrote its output to the local disk: YOLO saved annotated images to its run directory, and the NDVI node wrote a JPEG plot and a .npy array next to the code. Inside the LangGraph container those files died with the container, and paths in state pointed at nothing a client could open.
I changed every node to write to memory and upload to S3. YOLO now predicts with save=False, renders the annotated image into a buffer and stores it under outputs/yolo_<image>. The NDVI node puts the plot and the array into byte buffers and uploads them under outputs/ndvi_<image>. State now carries s3:// URIs instead of local paths. The carbon graph still passes NDVI from its tool to the next node through a local .npy file, and that is the next thing to move.
Stack
Python, LangGraph, LangChain tool-calling agents, Pydantic, OpenAI gpt-4o-mini, Claude 3 Haiku, tifffile, scikit-image, SciPy, NumPy, Ultralytics YOLO11, Open-Meteo, AWS S3 through boto3, Docker via the LangGraph CLI, DCP for distributed batch runs.