# Agent Lightning

Train and optimize AI agents using Microsoft's Agent Lightning framework with...

- Repository: https://github.com/coco-research/coco (797 stars)
- Category: AI and agents
- License: see repository
- Last updated: 2026-11-01
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add coco-research/coco --skill agent-lightning -g -a claude-code -y`
Codex: `npx skills add coco-research/coco --skill agent-lightning -g -a codex -y`
Cursor: `npx skills add coco-research/coco --skill agent-lightning -g -a cursor -y`
Gemini CLI: `npx skills add coco-research/coco --skill agent-lightning -g -a gemini-cli -y`

## Description

Train and optimize AI agents using Microsoft's Agent Lightning framework with reinforcement learning. Use when setting up agent training, instrumenting agents with tracing, configuring LightningStore, implementing reward functions, or optimizing prompts with RL/APO algorithms.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# Agent Lightning

Microsoft's framework for training AI agents with reinforcement learning, automatic prompt optimization, and supervised fine-tuning.

## Quick Start

### Installation

```bash
pip install agentlightning
```

For nightly builds:
```bash
pip install --upgrade --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ --pre agentlightning
```

### Minimal Integration (Zero Code Change)

Add `agl.emit_xxx()` helpers to your existing agent:

```python
import agentlightning as agl

# Your existing agent code
def my_agent(task):
    agl.emit_input(task)  # Track input
    
    response = llm.generate(task)
    agl.emit_output(response)  # Track output
    
    reward = evaluate(response)
    agl.emit_reward(reward)  # Track reward
    
    return response
```

## Core Concepts

### Architecture Flow

```
Agent (your code) → agl.emit_xxx() → Spans → LightningStore → Algorithm → Updated Resources
```

### Key Components

| Component | Purpose |
|-----------|---------|
| `LightningStore` | Central hub for traces, tasks, and resources |
| `Tracer` | Collects spans from agent execution |
| `Algorithm` | Consumes traces, produces improvements |
| `Trainer` | Orchestrates training loop |

## Instrumentation

### Emit Functions

```python
import agentlightning as agl

# Basic emissions
agl.emit_input(prompt)           # Track input to agent
agl.emit_output(response)        # Track agent output
agl.emit_reward(score)           # Track reward signal
agl.emit_tool_call(name, args)   # Track tool usage
agl.emit_tool_result(result)     # Track tool results
```

### Tracer Context

```python
from agentlightning import Tracer

tracer = Tracer(store=store)

with tracer.trace_context(task_id="task-123"):
    # All emissions within this context are grouped
    result = agent.run(task)
    
# Retrieve trace after execution
trace = tracer.get_last_trace()
```

### OpenTelemetry Integration

Agent Lightning integrates with OpenTelemetry:

The rest: https://github.com/coco-research/coco/blob/HEAD/skills/agent-lightning/SKILL.md
