# Portfolio Allocation and Rebalancing with the Portfolio-Review Skill in AI Berkshire

> Automate portfolio allocation and rebalancing with the AI Berkshire portfolio-review skill. Get live data, health checks, stress-testing, and expert-approved recommendations.

- Repository: [Xbt Lin/ai-berkshire](https://github.com/xbtlin/ai-berkshire)
- Tags: how-to-guide
- Published: 2026-07-02

---

**The `/portfolio-review` skill automates portfolio allocation and rebalancing by parsing your holdings, enriching them with live market data, and generating a six-step analysis that includes health checks, stress-testing, and cross-validated rebalancing recommendations approved by four value-investment masters.**

The `ai-berkshire` repository is a modular research framework that implements value-investment principles—drawing from **Warren Buffett (巴菲特)**, **Charlie Munger (芒格)**, **Duan Yongping (段永平)**, and **Li Lu (李录)**—through specialized AI agents. Its **portfolio allocation and rebalancing with portfolio-review skill** capability transforms single-stock research into comprehensive portfolio management via a declarative command that orchestrates data pipelines, scenario analysis, and master-level challenge protocols.

## Architecture of the Portfolio-Review Skill

### Three-Layer Design

The skill is defined in [`skills/portfolio-review.md`](https://github.com/xbtlin/ai-berkshire/blob/main/skills/portfolio-review.md) and follows a strict three-layer architecture:

- **Skill Layer** – A declarative command (`/portfolio-review`) that parses user inputs (percentages, share counts, cash, or saved files) and triggers the analysis pipeline.
- **Agent Layer** – Background agents launched via the internal **Task** tool fetch live market data, valuation metrics, recent news, and analyst expectations for each holding in parallel.
- **Tool Layer** – Python utilities in [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) perform exact decimal calculations, cross-validate data sources, and run `three-scenario` valuation projections.

This separation ensures financial rigor while maintaining the flexibility to challenge recommendations from multiple investment perspectives.

### Six-Step Analysis Pipeline

The pipeline defined in [`skills/portfolio-review.md`](https://github.com/xbtlin/ai-berkshire/blob/main/skills/portfolio-review.md) progresses through six discrete stages:

**1️⃣ Parse Holdings**
Inline parsing logic normalizes user input into a standardized table containing ticker symbols, share quantities, cost basis, current market price, percentage weight, and profit/loss calculations.

**2️⃣ Data Enrichment**
Parallel **Task** agents retrieve current prices, PE/PB ratios, recent earnings, and key corporate events. The [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) module runs `verify-valuation` checks to ensure data integrity before analysis begins.

**3️⃣ Single-Holding Health Check**
For each ticker, the skill asks three binary challenge questions (buy again? hold for five years? thesis still valid?) and produces a health-score table. This logic appears in **lines 56-60** of the skill file.

**4️⃣ Portfolio-Level Analysis**
The system constructs concentration matrices, correlation analyses, cash allocation summaries, and opportunity-cost comparisons. Key constraints—such as ensuring the largest position remains under 40%—are enforced here (**lines 65-78**).

**5️⃣ Stress-Testing and Optimization**
Scenario analysis runs against global recession, US-China conflict, and interest rate spike conditions. The `three-scenario` valuation method from [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) ranks holdings by expected annualized return (**lines 113-124 and 128-136**).

**6️⃣ Output Report Generation**
A structured markdown report is written to [`reports/portfolio-latest.md`](https://github.com/xbtlin/ai-berkshire/blob/main/reports/portfolio-latest.md) containing a concise health verdict, specific rebalancing actions (adds, cuts, cash moves), and a next-review reminder (**lines 52-73**).

## Core Source Files and Components

Understanding the file structure is essential for customizing or debugging the allocation pipeline:

- **[`skills/portfolio-review.md`](https://github.com/xbtlin/ai-berkshire/blob/main/skills/portfolio-review.md)** – The canonical skill specification defining input formats, the six-step pipeline, and output templates.
- **[`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py)** – Financial-validation utilities including `verify-valuation` for cross-source checks and `three-scenario` for probabilistic return projections.
- **[`reports/portfolio-latest.md`](https://github.com/xbtlin/ai-berkshire/blob/main/reports/portfolio-latest.md)** – Default output location for the generated portfolio analysis (created on each run).
- **[`scripts/sync-codex-skills.py`](https://github.com/xbtlin/ai-berkshire/blob/main/scripts/sync-codex-skills.py)** – Maintenance script that keeps the Codex skill artifacts in [`codex-skills/portfolio-review/SKILL.md`](https://github.com/xbtlin/ai-berkshire/blob/main/codex-skills/portfolio-review/SKILL.md) synchronized with the markdown source.
- **[`codex-skills/portfolio-review/SKILL.md`](https://github.com/xbtlin/ai-berkshire/blob/main/codex-skills/portfolio-review/SKILL.md)** – Generated Codex package derived from the source skill for IDE integration.

## Running Portfolio Allocation and Rebalancing

### CLI Usage with Claude Code

Install the Claude Code CLI and invoke the skill directly with your holdings:

```bash

# Install Claude client (see README for details)

npm install -g @anthropic-ai/claude-code

# Run the portfolio-review skill with a simple percentage list

claude /portfolio-review 腾讯30% 美团20% 茅台20% 英伟达15% 现金15%

```

### CLI Usage with Codex

If you have installed the skill set via the provided installation script:

```bash

# After installing the Codex skill set

codex /portfolio-review "腾讯 500股 @480HKD, 美团 800股 @130HKD, 现金 20%"

```

### Programmatic Invocation

You can trigger the skill from Python scripts using subprocess calls:

```python
import subprocess, json

cmd = [
    "codex",
    "/portfolio-review",
    "腾讯30%, 美团20%, 茅台20%, 英伟达15%, 现金15%"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)   # displays the generated portfolio-review report

```

## Summary

- The `/portfolio-review` skill automates **portfolio allocation and rebalancing** through a rigorous six-step pipeline defined in [`skills/portfolio-review.md`](https://github.com/xbtlin/ai-berkshire/blob/main/skills/portfolio-review.md).
- **[`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py)** provides the computational backbone with exact decimal arithmetic, `verify-valuation` cross-checks, and `three-scenario` probabilistic modeling.
- **Agent Layer** parallelism ensures real-time data enrichment without blocking the analysis workflow.
- Output reports in **[`reports/portfolio-latest.md`](https://github.com/xbtlin/ai-berkshire/blob/main/reports/portfolio-latest.md)** deliver actionable rebalancing instructions while preserving the four-master challenge philosophy—every recommendation is stress-tested against Buffett, Munger, Duan, and Li Lu's investment criteria.

## Frequently Asked Questions

### What input formats does the portfolio-review skill accept?

The skill accepts holdings expressed as percentages (`腾讯30%`), share counts with cost basis (`腾讯 500股 @480HKD`), absolute cash allocations (`现金15%`), or paths to saved portfolio files. The parsing logic in [`skills/portfolio-review.md`](https://github.com/xbtlin/ai-berkshire/blob/main/skills/portfolio-review.md) automatically normalizes these formats into a standardized internal table.

### How does the skill calculate rebalancing recommendations?

Rebalancing decisions combine **concentration limits** (e.g., ensuring a single position remains below 40% of total portfolio weight), **opportunity-cost matrices**, and **three-scenario valuation** rankings. The [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py) module calculates expected annualized returns across bear, base, and bull cases, then suggests adds, trims, or exits based on risk-adjusted expected value.

### Where is the portfolio analysis report saved?

By default, the skill generates a structured markdown report at **[`reports/portfolio-latest.md`](https://github.com/xbtlin/ai-berkshire/blob/main/reports/portfolio-latest.md)**. This file contains the health verdict for each holding, portfolio-level concentration analysis, stress-test results, and specific rebalancing actions with execution rationale.

### Can I integrate the portfolio-review logic into my own Python codebase?

Yes. While the skill is designed for CLI invocation via Claude Code or Codex, you can import functions directly from **[`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py)** for custom workflows. The `three-scenario` and `verify-valuation` utilities are pure Python and can operate independently of the agent layer, though you will need to implement your own data fetching logic if bypassing the Task agents.