Building a Low-Cost Mobile AI Agent: A Technical Breakdown of 21 Yuan in 7 Hours
This article explains how to build a reusable mobile AI agent on a limited budget by standardizing screen perception, constraining the action space, and normalizing failure recovery. It covers the four-layer architecture of perception, decision, execution, and feedback, along with practical cost-control techniques such as visual downsampling, control-tree prioritization, tiered model routing, caching, and step budgets. Developers will learn the core engineering capabilities behind low marginal-cost, cross-application mobile automation.
1. Background: Why Controlling a Phone Is a Key Agent Capability
AI agent research has been moving from pure text conversation toward interaction with real environments. On mobile devices, most capabilities are not exposed through public APIs, and the only interface users can reach is the graphical screen. As a result, letting a program “look at the screen, find controls, and tap buttons” like a human has become a general path to cross-application automation.
The idea in the source material essentially abstracts the interaction pattern of a certain class of multimodal content generation products and uses an agent to replace manual operation. This article does not focus on any specific product. Instead, it discusses a general engineering problem: how to build a reusable mobile operation agent with a limited budget.
This problem matters for three reasons:
- Coverage of long-tail applications: no need to integrate APIs one by one; in theory, any app with a UI can be operated.
- Low migration cost: the same perception and decision logic can be reused across apps and versions.
- Controllable marginal cost: the main expense comes from model calls and can be greatly reduced through architecture design.
2. Overall Architecture: A Perception-Decision-Execution-Feedback Loop
A usable mobile agent can be split into four layers:
- Perception layer: collect screen state (screenshots, control trees, window titles, etc.) and preprocess and compress it.
- Decision layer: understand the semantics of the current screen and output the next action based on the task goal.
- Execution layer: translate abstract actions into system-level events such as taps, swipes, input, and waits.
- Feedback layer: verify whether an action took effect, and roll back or replan on failure.
A sample main loop:
def run_agent(task: str, max_steps: int = 40):state = perceive() # Perception: screenshot + control treeplan = planner(task, state) # Planning: break into subgoalsfor step in range(max_steps):action = decide(plan, state) # Decision: output structured actionresult = execute(action) # Execution: inject eventsstate = perceive() # Feedback: observe againif verifier(task, state): # Verification: is the goal achieved?return Trueplan = replan(plan, action, result, state) # Replan on failurereturn False
3. Cost Control: The Engineering Logic Behind 21 Yuan in 7 Hours
Low cost does not depend on cheap credits from a particular vendor. It comes from systematic expense reduction, summarized in five points:
- Visual input downsampling: compress screenshot resolution and crop irrelevant regions to reduce visual tokens.
- Control tree first: when structured control information is enough to make a judgment, skip the screenshot; text is cheaper and more stable.
- Tiered model routing: use a small model for simple judgments and upgrade to a large model only for complex reasoning.
- Caching and deduplication: when the same screen state hits the cache, reuse the historical decision directly.
- Step budget and early stopping: set a maximum step count and confidence threshold to avoid wasting money in ineffective loops.
A simplified routing strategy:
def decide(plan, state):if state.has_control_tree and state.simple_match(plan):return state.rule_based_action(plan) # Zero model callsmodel = "small" if state.ambiguity < 0.3 else "large"return call_model(model, plan, state) # Upgrade on demand
In practice, the higher the control tree hit rate and the greater the screen repetition, the more clearly the per-task cost drops. This is why “task templating + state caching” is more effective than simply switching to a cheaper model.
4. Perception Layer Design: Turning the Screen into Computable Data
Perception quality directly determines the upper limit of decision quality. Three things are recommended:
- Multi-source fusion: screenshot + control tree + app title, cross-checking each other.
- Object detection for localization: detect icons and input fields and output normalized coordinates to avoid hardcoding resolution.
- Text extraction: recognize on-screen text to form a “list of clickable elements” for the decision layer to choose from.
{"screen_id": "s_128","candidates": [{"text": "Search", "bbox": [0.12, 0.08, 0.30, 0.13], "type": "button"},{"text": "Input field", "bbox": [0.10, 0.16, 0.90, 0.22], "type": "edit"}]}
Turning the screen into a structured list of “candidate elements + semantic labels + coordinates” is a key step for reducing cost and improving stability.
5. Decision and Execution: A Structured Action Space
Do not let the model improvise freely. Constrain it to a limited action set, for example:
tap(x, y),swipe(x1,y1,x2,y2,duration),input(text),back(),wait(ms),launch(app).
Constraining the action space brings three benefits: output is parseable, verifiable, and rollback-friendly. Also require the model to return “action + expected result” so the feedback layer can make assertions.
ACTION_SCHEMA = {"action": "tap | swipe | input | back | wait | launch","args": {...},"expect": "A search results list appears"}
6. Stability Engineering: Failure Recovery and Idempotent Design
Real mobile environments are noisy: pop-ups, slow loading, network jitter, and version differences. You must assume that any step can fail.
- Idempotent actions: repeated taps produce no side effects, or check the state before executing.
- Assertion checks: compare against expectations after each step; retry or roll back on mismatch.
- Layered retries: screen-level retry (wait for loading), task-level retry (replan), session-level termination (report failure).
- Full-chain logging: record screenshot hashes, actions, elapsed time, and token consumption for review and tuning.
7. Scalability and Security Boundaries
- Abstract app adaptation layer: encapsulate differences between apps as “capability descriptions” while keeping the core loop stable.
- Least privilege: grant only necessary permissions, and require secondary confirmation for sensitive operations.
- Data compliance: screenshots may contain private information, so desensitize locally and minimize retention.
- Observability: combine logging services, monitoring alerts, and object storage to archive runtime data and form a continuous optimization loop; for elastic execution, use a container platform to run concurrent tasks.
8. Conclusion
The significance of “7 hours, 21 Yuan” is not a specific number but the validation of an engineering path: standardize screen operations, tier model calls, and normalize failure handling, and you can obtain cross-application automation at extremely low marginal cost. For developers, the real moat is not calling a particular model, but three foundational engineering capabilities: perception quality, action constraints, and feedback verification.
