use serde_json::{Value, json};
use super::adapter::{RegionAdapter, boxed, image_index, quad};
use crate::backend::InputLimits;
use crate::budget::ImageTokens;
use crate::geometry::CoordFrame;
use crate::traits::ImageRegion;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClaudeVisionTier {
High,
Standard,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClaudePixels {
tier: ClaudeVisionTier,
}
impl ClaudePixels {
pub fn new(tier: ClaudeVisionTier) -> Self {
Self { tier }
}
pub fn for_model(model: &str) -> Self {
Self::new(tier_for_model(model))
}
pub fn tier(&self) -> ClaudeVisionTier {
self.tier
}
}
fn tier_for_model(model: &str) -> ClaudeVisionTier {
if crate::screen::claude_high_tier(model) {
ClaudeVisionTier::High
} else {
ClaudeVisionTier::Standard
}
}
impl RegionAdapter for ClaudePixels {
fn name(&self) -> &str {
"claude-pixels"
}
fn frame(&self) -> CoordFrame {
CoordFrame::SentPixels
}
fn limits(&self) -> InputLimits {
match self.tier {
ClaudeVisionTier::High => InputLimits::long_edge(2576).with_max_pixels(3_605_000),
ClaudeVisionTier::Standard => InputLimits::long_edge(1568).with_max_pixels(1_150_000),
}
}
fn image_tokens(&self) -> ImageTokens {
match self.tier {
ClaudeVisionTier::High => ImageTokens::Claude,
ClaudeVisionTier::Standard => ImageTokens::Fixed { tokens: 1_568 },
}
}
fn region_schema(&self) -> Value {
json!({
"type": "object",
"properties": {
"label": {"type": "string", "description": "What the region is."},
"box": {
"type": "array",
"items": {"type": "number"},
"description": "[x_min, y_min, x_max, y_max] in pixels of the image."
}
},
"required": ["label", "box"],
"additionalProperties": false
})
}
fn instructions(&self, sizes: &[(u32, u32)]) -> String {
let images = match sizes {
[(w, h)] => format!("The image is {w}x{h} pixels."),
many => many
.iter()
.enumerate()
.map(|(i, (w, h))| format!("Image {i} is {w}x{h} pixels."))
.collect::<Vec<_>>()
.join(" "),
};
format!(
"{images} Give each region's `box` as [x_min, y_min, x_max, y_max] in pixels of \
that image, origin at the top-left corner, x to the right and y down, the box \
tight around the region."
)
}
fn parse_region(
&self,
item: &Value,
sizes: &[(u32, u32)],
) -> Result<Option<ImageRegion>, String> {
let image = image_index(item, sizes)?;
let Some(raw) = quad(item, &["box", "bbox", "bbox_2d"])? else {
return Ok(None);
};
Ok(Some(boxed(self.frame(), raw, item, image)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tiers_follow_the_model() {
for high in [
"claude-sonnet-5-5",
"claude-haiku-5-5",
"claude-opus-5-5",
"claude-opus-4-7",
"claude-opus-4-8",
"claude-sonnet-5",
"claude-fable-5-2",
"anthropic.claude-sonnet-5-5",
"claude-opus-4-7@20260101",
] {
assert_eq!(tier_for_model(high), ClaudeVisionTier::High, "{high}");
}
for standard in ["claude-haiku-4-5", "claude-sonnet-4-6", "claude-opus-4-6", "claude-3-7"] {
assert_eq!(tier_for_model(standard), ClaudeVisionTier::Standard, "{standard}");
}
}
#[test]
fn token_formula_matches_count_tokens() {
let claude = ClaudePixels::new(ClaudeVisionTier::High);
for (size, measured) in [
((960, 540), 712),
((1000, 1000), 1308),
((1001, 1001), 1308),
((1568, 1018), 2084),
((2356, 1530), 4687),
] {
assert_eq!(claude.image_tokens().tokens(size), measured - 12, "{size:?}");
}
}
#[test]
fn the_limit_keeps_every_aspect_within_the_token_cap() {
let limits = ClaudePixels::new(ClaudeVisionTier::High).limits();
for (w, h) in [(3024, 1964), (5000, 5000), (2576, 2576), (8000, 1000), (2000, 2000)] {
let (fw, fh) = limits.fit((w, h)).unwrap_or((w, h));
assert!(fw.max(fh) <= 2576);
assert!(u64::from(fw.div_ceil(28)) * u64::from(fh.div_ceil(28)) <= 4784, "{w}x{h}");
}
}
}