Glean 拾遗
Daily /2026-09-21 / AI Coding Is a Framework, Not a Library

AI Coding Is a Framework, Not a Library

Source www.piglei.com Glean’d 2026-09-21 06:00 Read 4 min
AI summary

Piglei argues that AI coding tools are better understood as a framework than a library. Frameworks own the program's overall structure and buy convenience at low upfront cognitive cost; AI tools do the same, with natural language replacing code as the input. Using Django REST Framework as the case study, he shows a four-line ModelViewSet generating a full CRUD API, then details what customizing a create response or adding list filters actually costs: rewriting get_queryset and stacking if/else patches. Dropping to a plain ViewSet makes the code longer but surfaces the hidden cognitive debt. Two framework problems persist with AI: abstraction leaks, when prompts fail and you must debug down to variable names, and loss of control, as in vibe coding where the agent owns the structure. His advice: treat AI as a library, find the prompt sweet spot, design the structure yourself, encode constraints in AGENTS.md, and review generated code.

Original · 4 min
www.piglei.com ↗
§ 1

§ 2

The more I use AI coding tools, the more strongly I feel that AI coding is a kind of "framework."

使用 AI 类编程工具越多,我便越来越强烈地感觉到:AI 编程是一种“框架(Framework)”。

§ 3

Frameworks are an old friend to every programmer. They are usually designed for a specific domain and can dramatically boost coding efficiency. Take REST API services: a mature framework such as Django REST Framework lets you define a data model and a view class, and you get a fully functional CRUD API service out of it.

框架是每位程序员的老朋友,它通常针对特定领域所设计,能极大提升编码效率。以 REST API 服务为例,一些成熟的框架(比如 Django REST Framework)能做到仅需定义数据模型和视图类,便生成一套功能完备的 CRUD API 服务。

§ 4

AI coding tools, like traditional frameworks, are a kind of "lever": they let people move enormous functionality with a tiny input. The only difference is the type of input. A framework needs code (or configuration); AI needs nothing more than a plain-language prompt — "build me a bookstore website."

AI 编程工具和传统框架一样,都是一种“杠杆”,它们允许人们用少量输入撬动庞大的功能。二者区别仅在于输入类型不同,框架需要代码(或配置),而 AI 仅需一句简单的自然语言提示词——“写一个书店网站”。

§ 5

As a new kind of framework, AI coding is so convenient that it wipes the floor with every framework humans have ever invented. But the problems frameworks are born with have not gone away.

作为一种新型框架,“AI 编程”所带来的便利性,足以把人类从前发明过的任何一种框架按在地上摩擦,但是,框架天生所具备的问题并未消失。

§ 6
  1. Leaky abstractions

All frameworks share one trait: they offer very high-level abstractions to cut down the work needed for a specific feature. Django's ORM, for instance, gives you the "data model" abstraction, sparing you from writing SQL by hand or designing data validation.

  1. 抽象泄露

所有框架都有一个共性,它们提供了极高级的抽象,以降低实现特定功能所需的工作量。比如,Django 的 ORM 提供了“数据模型”这层抽象,免去了手写 SQL 语言、设计数据校验等繁重工作。

§ 7

Unfortunately, all abstractions leak.

A Django beginner ships a site and the customer complaints explode: the list page loads painfully slowly. Open the database monitoring dashboard and you'll find that this seemingly simple page fires 400 database queries on every visit. To fix it, he has to get his hands dirty, tear open the ORM abstraction, understand how attributes get loaded, and grasp the truth about the "N+1 problem."

但不幸的是,抽象都会泄露。

一名 Django 初学者编写的网站上线后,客户投诉爆表,因为列表页加载极为缓慢。此时,打开数据库监控面板,他会发现这个看似简单的页面,每访问一次就会发起 400 次数据库查询。要解决该问题,他必须弄脏双手,撕开 ORM 这个抽象层,弄懂“属性如何被加载?”,搞清楚“N+1 问题”的真相。

§ 8

With AI coding tools, the leak shows up at the moment we have to abandon the easy natural-language prompt — "implement feature XXX" — and instead say, "your understanding of the state transitions of the items variable is wrong."

Needless to say, today's AI still has an intelligence ceiling. When natural language can't drive the AI to do the job accurately, breaking the abstraction, opening that dusty IDE, and writing prompts precise down to variable names to help the AI find the root cause is our only option.

而使用 AI 编程工具,“抽象泄露”则发生在我们不得不抛弃轻松的自然语言提示词——“实现 XXX 功能”,转而说出“你对 items 变量的状态转换的理解不对”的时刻。

毋庸讳言,现阶段的 AI 仍存在智能瓶颈。当自然语言无法驱动 AI 准确完成工作时,打破抽象,打开已布满灰尘的 IDE,用精确到变量名的提示词帮 AI 找到根因,是我们的唯一选择。

§ 9
  1. Losing control

All patterns for reusing code fall into roughly two kinds: framework and library.

Whether something is a framework or a library comes down to one question: who controls the overall structure of the program?

  1. 丧失控制权

世上实现代码复用的所有模式,大致可被分为两种:框架(framework)和库(library)。

要区分一个东西是框架还是库,关键在于找到“谁控制着程序的整体结构?” 这个问题的答案。

§ 10

With a framework, control sits firmly in the framework's hands. The program you write is a piece fitted into the grand framework program. It's a bit like finishing a cartoon drawing: every element is already outlined in light gray, and you just fill different areas with different colors.

With a library, control stays with you. You choose and coordinate different libraries to build the whole program. It's like playing with bricks: you have thousands of bricks and modules at hand, and you assemble them into whatever shape you want.

使用框架,控制权牢牢掌握在框架手中,你所编写的程序,是镶嵌在伟岸的框架程序中的一部分。这有点像是去完成一副卡通图,所有元素都已用浅灰色线条勾边,你只负责给不同部位涂上不同颜色。

而使用库,控制权则仍掌握在你手里。你负责调配和使用不同的库,来搭建起整个程序。这像是玩积木,手边有千千万万个积木和模组,你负责把它们组装成想要的样子。

§ 11

What harm does losing control do? Mostly, it hurts modifiability.

With a framework, when the program needs a custom feature, the lack of control means the work depends on whether the framework supports that customization. If it does, the whole thing goes like a hot knife through butter. If it doesn't, then unfortunately you may burn tons of time on a simple requirement.

丧失控制权会带来哪些危害?这主要体现在可修改性上。

使用框架时,当程序需要定制一项功能,由于缺少控制权,工作的开展依赖于框架其是否支持该自定义选项。如果支持,那么整个过程会像热刀切开黄油一样丝滑。但假若框架并不支持,那么很不幸,你极可能在一个简单需求上花掉成吨时间。

§ 12

To be fair, on the "control" axis, AI coding isn't inherently a framework or a library. But there's no denying that the most popular trend right now is to use it as a framework. In vibe coding, for example, the human only supplies vague natural-language prompts and doesn't care at all about the program's entry points, exit points, or overall structure — the AI agent, acting as the framework, controls everything.

平心而论,在“控制权”维度上,AI 编程还称不上天生属于“框架”还是“库”。但不可否认的是,当下最流行的一种趋势就是把它当成框架来使用。比如在 Vibe Coding 时,人类仅负责提供模糊的自然语言提示词,毫不关心程序的入出口与整体结构,一切由 AI Agent 这个框架所掌控。

§ 13

Cognitive cost: what DRF teaches us

Why do people tend to treat AI as a framework? That's an interesting question.

I think the key is one phrase: cognitive cost. The traits frameworks are born with hint at something: you can build the most complex features at the lowest cognitive cost. And humans are born to take the easy path — doing more with less thinking is a goal we chase by nature.

认知成本:DRF 的启示

为何人们倾向于将 AI 当成框架?这是个有趣的问题。

我认为答案的关键在于一个词:认知成本。框架与生俱来的特质,给了我们一种暗示:你可以用最低的认知成本实现最复杂的功能。 而人类生来就爱“省力”,少动脑而完成更多,是我们与生俱来追求的目标。

§ 14

Take DRF (Django REST Framework). In DRF, implementing a set of CRUD APIs for one kind of data model takes just these four lines:

class AccountViewSet(viewsets.ModelViewSet):
    queryset = Account.objects.all()
    serializer_class = AccountSerializer
    permission_classes = [IsAccountAdminOrReadOnly]

One class, three attributes, minimal cognitive cost, a full set of RESTful APIs — by any measure, that's a very good deal.

拿 DRF(Django REST Framework) 来举例。在 DRF 框架中,为一类数据模型实现一套 CRUD API,只需编写以下 4 行代码:

class AccountViewSet(viewsets.ModelViewSet):
    queryset = Account.objects.all()
    serializer_class = AccountSerializer
    permission_classes = [IsAccountAdminOrReadOnly]

一个 class,三个属性,极低的认知成本,全套 RESTful API,怎么看这都是一门极其划算的生意。

§ 15

But as noted earlier, a framework's convenience is a double-edged sword, and the "leaky abstraction" and "loss of control" that come with frameworks are unavoidable.

Suppose we now need to change the API so that create returns a different response body and list gets extra filter conditions. How would you do that on top of the code above? The answer: rewrite several methods, including get_queryset, then patch the entire ModelViewSet into an unrecognizable mess with a pile of if/else.

但是,正如前面所提到的,框架的便利性是一把双刃剑,伴随框架出现的“抽象泄露”和“控制权丧失”问题不可避免。

做个假设,现在我们需要调整 API,让 create 方法返回不同的响应体,为 list 方法增加额外的过滤条件,基于以上代码该怎么做?答案是,先重写包含 get_queryset 在内的多个方法,再用一批 if/else 补丁将整个 ModelViewSet 爆改到面目全非。

§ 16

Is there a better way? Certainly. We can drop the high-level ModelViewSet entirely and use a plain ViewSet with no magic at all, composing modules to achieve exactly the same thing.

class AccountViewSet(viewsets.ViewSet):
    permission_classes = [IsAccountAdminOrReadOnly]

    def list(self, request):
        # custom filtering logic for list
        qs = Accounts.objects.filter(...)
        serializer = AccountSerializer(qs, many=True)
        return Response(serializer.data)

    def create(self, request):
        # custom response structure for create
        serializer = AccountForCreationSerializer(obj)
        return Response(AccountForCreationSerializer(obj).data)

既然如此,有没有更好的办法?答案是肯定的,我们完全可以弃用高级的 ModelViewSet,仅采用不附带任何魔法的 ViewSet,完全基于模块的组合来实现相同功能。

class AccountViewSet(viewsets.ViewSet):
    permission_classes = [IsAccountAdminOrReadOnly]

    def list(self, request):
        # 针对 list 的特殊过滤逻辑
        qs = Accounts.objects.filter(...)
        serializer = AccountSerializer(qs, many=True)
        return Response(serializer.data)

    def create(self, request):
        # 针对 create 的特殊响应结构
        serializer = AccountForCreationSerializer(obj)
        return Response(AccountForCreationSerializer(obj).data)
§ 17

By comparison, the most obvious change is that there is more code, and wordier code at that. But the bigger change happens at the level of "cognitive cost."

In the ModelViewSet version, cognitive cost looks low on the surface, but much of it is hidden — it exists as hidden cognitive debt. Later, when requirements change, that debt makes implementing features enormously painful. After restructuring the code, the hidden debt surfaces, and the code's visibility and maintainability improve markedly.

相比起来,新方案最直观的变化是代码量变多了,也显得更加啰嗦,但更大的改变其实发生在“认知成本”层面上。

在 ModelViewSet 版代码里,认知成本表面上很低,但更多成本其实被藏了起来,作为一种隐藏的认知债务所存在。后续,当需求发生变化时,这些债务给我们实现功能带来了巨大的麻烦。调整代码结构后,原本隐藏的债务浮出了水面,代码的可视性和可维护性也得到了有效提升。

§ 18

If we look at the example above through the "framework vs library" lens, the new code is organized more like a library — a person owns the program and organizes the whole feature — rather than a framework, where a person is merely a servant filling in the blanks of ModelViewSet.

So we can see that framework and library aren't a strict binary classification of things; they're more like two different ways of thinking. Even inside a huge framework like DRF, there are all sorts of code organization patterns — some lean framework, others clearly lean library.

假如以“框架和库”这个设定来观察上面的案例,新代码的组织模式更接近于“库”——人作为程序的主人去组织整个功能,而非“框架”——人仅作为 ModelViewSet 的仆人去填补空缺。

至此我们可以看到,框架与库并非一种对物品的二元化严格分类,而更像两种不同的思维方式。因为即便是在 DRF 这个庞大的框架中,也存在各种不同的代码组织模式,一些偏框架,另一些则明显偏向库。

§ 19

Maybe it's time to treat AI coding as a library

Back to AI coding. Treating AI coding as a framework pushes us into framework thinking: always chasing shorter prompts, caring less about the code, spending less cognitive cost to reach the goal.

But as the earlier example shows, under that mindset cognitive debt keeps piling up, and the framework's built-in problems — leaky abstraction and loss of control — will hurt us badly down the road.

So why not shift your thinking and treat AI coding as a "library"? Just as with any other library, we own the program and call on it to get work done.

也许,该把 AI 编程当成一种库

回到 AI 编程。将 AI 编程作为一种框架,会引导我们采用框架式思维,不断追求编写更短的提示词、对代码付出更少的关注,付出更少的认知成本来达成目的。

然而,就像前面的例子所展示的,在这种思维模式下,认知债务将不断堆积,框架与生俱来的“抽象泄露”与“控制权丧失”问题,也将在未来对我们产生严重危害。

既然如此,何不转变思维,将 AI 编程当成一种“库”?就像去使用任何其他库一样,我们作为程序的主人,调用它来完成工作。

§ 20

That shift in mindset might mean:

  • Stop chasing "write less, do more": fewer prompts (code) for more features looks lovely, but it also means a pile of cognitive debt accumulating;
  • Find the "sweet spot" for writing prompts, paying relatively little — not the absolute minimum — cognitive cost;

这种思维模式的转变,可能意味着:

  • 不再追求“写更少实现更多”:用更少的提示词(代码)实现更多功能,看上去很美,但也意味着大量的认知债务随之累积;
  • 找到编写提示词的“甜蜜区”,付出 相对较少 而非绝对意义上的最少的认知成本;
§ 21
  • Focus on program structure: more than in the pre-AI era, you now need to care about the program's overall structure, acting as chief architect to design the whole thing and internalizing the right structure and constraints into AGENTS.md;

  • More precise prompts: based on an understanding of the existing program, write more precise prompts to steer the AI, instead of letting it loose and letting AI run everything;

  • Review the code: even with the same framework, when a thorny problem hits, someone who has read the docs thoroughly is more effective than a greenhorn. If AI-written code counts as a framework, then you should review that code, so that when the inevitable "leaky abstraction" strikes, the damage stays as small as possible.

  • 关注程序结构: 比起在前 AI 时代,你现在可能更需要关注程序的整体结构,作为总设计师去设计整个程序,将正确的结构和约束内化到 AGENTS.md 中;

  • 更精准的提示词: 在理解已有程序的基础上,编写更精准的提示词来引导 AI 完成工作,而不是任其发挥,让 AI 主导一切;

  • 审查代码: 即便使用同一种框架,在遇到棘手问题时,一位熟读框架文档的人也会比另一位愣头青更有效率,如果把 AI 编写的代码归为框架,那么你应该去审查这份代码,从而在不可避免的“抽象泄露”发生时,将其危害降到最低。

§ 22

There's no doubt that AI coding is a revolutionary advance. It lets us write programs from a vantage point that previously existed only in the imagination. But AI coding is not magic: like any framework in history, behind its extreme convenience lurk multiple risks — leaky abstractions, loss of control, cognitive debt.

毋庸置疑,AI 编程是一项革命性的进步,它的出现,让我们可以站在一个之前只存在于想象中的思维高度来编写程序,但 AI 编程毕竟不是魔法,如同历史上任何一个框架,极度便利的背后隐藏着抽象泄露、控制权丧失及认知债务等多重风险。

§ 23

So AI coding is still no silver bullet; it can't truly free us from cognitive cost. But until the real silver bullet arrives, recognizing the limits of AI coding as a new kind of "framework" — and steering it with the mental model of a "library" — may be our best option.

所以,AI 编程仍然不是银弹,它无法将我们从认知成本中真正解放出来。然而,在真正的银弹出现前,认识到 AI 编程作为一种新型“框架”的局限性,并适当使用“库”的心智模型来驾驭它,或许是我们的最佳选择。

Open source ↗